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排序后返回序号)