mysql数据库查询如何添加排名序号

mysql中排序后根据排序的内容显示序号,需要在子查询中select @rownum:=0,只有外层的@rownum并不会起作用。

SELECT
	a.num,
	a.content,
	t.tagname,
	@rownum := @rownum + 1 AS sortorder 
FROM
	( SELECT count( * ) AS num, content, @rownum := 0 FROM action WHERE actiontype = 'tag' GROUP BY content ) a
	LEFT JOIN tag t ON a.content = t.tagid 
ORDER BY
	a.num DESC 
	LIMIT 10;

在没有子查询的情况下,可以在外面包一层查询。

SELECT
	n.*,
	@rownum := @rownum + 1 AS sortorder 
FROM
	(
	SELECT
		COUNT( * ) AS num,
		vendor 
	FROM
		device 
	WHERE
		LENGTH( vendor ) > 0 
	GROUP BY
		vendor 
	ORDER BY
		num DESC 
		LIMIT 10 
	) n,
	( SELECT @rownum := 0 ) r;

 

你可能感兴趣的:(mysql数据库,mysql,排名,序号)