Mysql的给分组加行号


(1)第一次执行时分组行号不起作用

  1.按指定排序获取数据:

select * from table order by type

  2.给获取的结果增加统计字段:

@num:=IF(@groups=`type`,@num+1,IF(@groups:=`type`,1,1)) as total 

  3.组合语句:

SELECT wid,name,type,@num:=IF(@groups=`type`,@num+1,IF(@groups:=`type`,1,1)) as total FROM w_websites ORDER BY type 

 

(2)分组加行号

SELECT a.type,IF(@v<>a.type,@r:=1,@r:=@r+1) as total,IF(@v<>a.type,@v:=a.type,@v) as ctype
FROM (SELECT @r:=0,@v:=-1) as c,w_websites a 
INNER JOIN xxx b
ON xxx
WHERE xxx
ORDER BY xxx

  

链接:http://www.qianxingweb.com 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM