首页 > 代码库 > mysql - 行号
mysql - 行号
1. 初始化数据 - 列唯一
DROP TABLE IF EXISTS `sales`;CREATE TABLE `sales` ( `empid` VARCHAR(10) NOT NULL, `mgrid` VARCHAR(10) NOT NULL, `qty` INT(11) NOT NULL, PRIMARY KEY (`empid`)) ENGINE=INNODB DEFAULT CHARSET=utf8;/*Data for the table `sales` */INSERT INTO `sales`(`empid`,`mgrid`,`qty`) VALUES (‘A‘,‘Z‘,300),(‘B‘,‘X‘,100),(‘C‘,‘X‘,200),(‘D‘,‘Y‘,200),(‘E‘,‘Z‘,250),(‘F‘,‘Z‘,300),(‘G‘,‘X‘,100),(‘H‘,‘Y‘,150),(‘I‘,‘X‘,250),(‘J‘,‘Z‘,100),(‘K‘,‘Y‘,200);
2. 查询 - 列唯一
SELECT empid,( SELECT COUNT(*) FROM sales T1 WHERE T1.empid <= T2.empid) AS rownum FROM sales T2;
运行结果:
3. 按照qty和empid顺序生成行号 - 列唯一
SELECT empid,qty,( SELECT COUNT(*) FROM sales T1 WHERE T1.qty < T2.qty OR (T1.qty=T2.qty AND T1.empid <= T2.empid) ) AS rownum FROM sales T2 ORDER BY qty,empid;
运行结果:
4. 初始化数据 - 列重复
CREATE TABLE `t` ( `a` CHAR (3)); INSERT INTO `t` (`a`) VALUES(‘X‘);INSERT INTO `t` (`a`) VALUES(‘X‘);INSERT INTO `t` (`a`) VALUES(‘X‘);INSERT INTO `t` (`a`) VALUES(‘Y‘);INSERT INTO `t` (`a`) VALUES(‘Y‘);INSERT INTO `t` (`a`) VALUES(‘Z‘);
5. 查询 - 列重复
SELECT n.a,n.a+smaller AS rownum, C.a FROM( SELECT a, COUNT(*) AS countt, ( SELECT COUNT(*) FROM t AS B WHERE B.a < A.a ) AS smaller FROM t AS A GROUP BY a) AS C, nums AS nWHERE n.a <= countt
运行结果:
mysql - 行号
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。