更新了部分文档
parent
c6ef269a37
commit
3bcf6bf0f7
Binary file not shown.
Before Width: | Height: | Size: 764 KiB After Width: | Height: | Size: 803 KiB |
|
@ -159,14 +159,14 @@ select * from (
|
||||||
) `temp` where `rank` between 4 and 6;
|
) `temp` where `rank` between 4 and 6;
|
||||||
```
|
```
|
||||||
|
|
||||||
> **说明**:上面使用的函数`row_number()`可以为每条记录生成一个行号,在实际工作中可以根据需要将其替换为`rank()`或`dense_rank()`函数,三者的区别可以参考官方文档或阅读[《通俗易懂的学会:SQL窗口函数》](https://zhuanlan.zhihu.com/p/92654574)进行了解。在MySQL 8以前的版本,我们可以通过下面的方式来完成类似的操作。
|
上面使用的函数`row_number()`可以为每条记录生成一个行号,在实际工作中可以根据需要将其替换为`rank()`或`dense_rank()`函数,三者的区别可以参考官方文档或阅读[《通俗易懂的学会:SQL窗口函数》](https://zhuanlan.zhihu.com/p/92654574)进行了解。在MySQL 8以前的版本,我们可以通过下面的方式来完成类似的操作。
|
||||||
>
|
|
||||||
> ```SQL
|
```SQL
|
||||||
> select `rank`, `ename`, `sal` from (
|
select `rank`, `ename`, `sal` from (
|
||||||
> select @a:=@a+1 as `rank`, `ename`, `sal`
|
select @a:=@a+1 as `rank`, `ename`, `sal`
|
||||||
> from `tb_emp`, (select @a:=0) as t1 order by `sal` desc
|
from `tb_emp`, (select @a:=0) as t1 order by `sal` desc
|
||||||
> ) as `temp` where `rank` between 4 and 6;
|
) as `temp` where `rank` between 4 and 6;
|
||||||
> ```
|
```
|
||||||
|
|
||||||
例子2:查询每个部门月薪最高的两名的员工的姓名和部门名称。
|
例子2:查询每个部门月薪最高的两名的员工的姓名和部门名称。
|
||||||
|
|
||||||
|
@ -180,13 +180,13 @@ from (
|
||||||
) as `temp` natural join `tb_dept` where `rank`<=2;
|
) as `temp` natural join `tb_dept` where `rank`<=2;
|
||||||
```
|
```
|
||||||
|
|
||||||
> 说明:在MySQL 8以前的版本,我们可以通过下面的方式来完成类似的操作。
|
说明:在MySQL 8以前的版本,我们可以通过下面的方式来完成类似的操作。
|
||||||
>
|
|
||||||
> ```SQL
|
```SQL
|
||||||
> select `ename`, `sal`, `dname` from `tb_emp` as `t1`
|
select `ename`, `sal`, `dname` from `tb_emp` as `t1`
|
||||||
natural join `tb_dept`
|
natural join `tb_dept`
|
||||||
where (
|
where (
|
||||||
select count(*) from `tb_emp` as `t2`
|
select count(*) from `tb_emp` as `t2`
|
||||||
where `t1`.`dno`=`t2`.`dno` and `t2`.`sal`>`t1`.`sal`
|
where `t1`.`dno`=`t2`.`dno` and `t2`.`sal`>`t1`.`sal`
|
||||||
)<2 order by `dno` asc, `sal` desc;
|
)<2 order by `dno` asc, `sal` desc;
|
||||||
> ```
|
```
|
Loading…
Reference in New Issue