mysql 显示行号,以及分组排序
2016-03-07 11:18
746 查看
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
相关文章推荐
- MySQL开户、权限设置、建库流程及常用操作
- MySQL root用户被删除解决办法
- MySQL索引原理及慢查询优化
- windows mysql设置 账户root 密码
- mysql中DATE_FORMAT()和str_to_date()简介
- mysql5.6安装最佳实践
- MySQL rename database如何做?
- PASSION之安装mysql常见问题
- mysqlbinlog flashback 5.6完全使用手册与原理
- SQL Server 迁移数据到MySQL
- 不同版本的mysql字符集的默认编写
- MySQL数据包导入或接受失败异常处理
- MySQL数据库文件目录
- MySQL查询重复字段,及删除重复记录的方法
- Mysql数据表信息插入到一个新的总数据表,总数据表id(AI自增)没有从1开始的解决
- mysql行列转换方法总结
- mysql之sql编程
- Mysql5.0没有nvarchar,national
- Canal+Otter - 前日篇(2)
- 设置MySQL开机自动启动的方法