CREATE TABLE salary ( id INT(15) NOT NULL AUTO_INCREMENT COMMENT '主键', ename VARCHAR(20) NOT NULL COMMENT '员工姓名', department VARCHAR(30) NOT NULL COMMENT '部门名称', money INT(6) DEFAULT NULL COMMENT '工资', PRIMARY KEY (id), UNIQUE KEY id (id) ) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
INSERT INTO salary(id,ename,department,money) VALUES (1,'李德','技术部',6000),(2,'刘明','人事部',8000),(3,'张泉','技术部',9000),(4,'王亮','市场部',7000),(5,'郑强','财务部',5000),(6,'赵庆','人事部',4000),(7,'王林','技术部',6000),(8,'宋翔','市场部',3000),(9,'秦力','市场部',5000),(10,'蒋昆','财务部',2000);
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r < 5;
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r < 5 ORDER BY @r;
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r > 1;
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r > 1 ORDER BY @r;
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r = 1;
SET @r:=1;
SELECT *, @r:=@r+1 FROM salary HAVING @r = 1 ORDER BY money;
因为你加了Order by ,并且为什么用Having不用where呢?
SELECT *, @r:=@r+1 FROM salary HAVING @r = 1;
这里为啥用haveing? 不用where
having结合gropu by用的