mysql 显示行号,以及分组排序
mysql 显示行号,以及分组排序建表:CREATE TABLE `my_tb` (`id` int(11) NOT NULL AUTO_INCREMENT,`parent_code` varchar(255) DEFAULT NULL,`code` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`))
·
建表:
CREATE TABLE `my_tb` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_code` varchar(255) DEFAULT NULL, `code` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
初始数据:
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('01', '001'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('01', '002'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('02', '001'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('01', '003'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('02', '002'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('03', '001'); INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('04', '001');

查询行号:
-- 生成 行号 select @r:=@r+1 as row_num , a.* from my_tb a ,(select @r:=0) b

显示分组号:
-- 生成 分组排序号 select @group_row:=CASE when @parent_code=a.parent_code then @group_row+1 else 1 end as groupRow, @parent_code:=a.parent_code as parent_code, a.code from my_tb a ,( select @group_row:=1, @parent_code:='') as b ORDER BY a.parent_code , a.code

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)