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`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

初始数据:

INSERT INTO `my_tb` (  `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');
INSERT INTO `my_tb` ( `parent_code`, `code`) VALUES ('', '');

mysql  显示行号,以及分组排序

查询行号:

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

mysql  显示行号,以及分组排序

显示分组号:

-- 生成 分组排序号

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

mysql  显示行号,以及分组排序

上一篇:wuzhicms页面报错 Notice 错误,如何关闭错误显示!


下一篇:Mysql编辑工具中使用(Navicat查询结果显示行号)