mysql 自连接
来源:互联网 发布:matlab socket编程实例 编辑:程序博客网 时间:2024/06/05 10:09
现有一Employee 表,详情如下
mysql> select * from Employee;+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 1 | Joe | 70000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 4 | Max | 90000 | 1 || 5 | Janet | 69000 | 1 || 6 | Randy | 85000 | 1 |+----+-------+--------+--------------+6 rows in set (0.00 sec)
求每一个部门DepartmentId中Salary最高的前三名
mysql> select * from Employee e1 where (select count(distinct Salary) from Employee e2 where e1.DepartmentId=e2.DepartmentId and e1.Salary<e2.Salary) < 3 order by e1.DepartmentId,e1.Salary desc;+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 4 | Max | 90000 | 1 || 6 | Randy | 85000 | 1 || 1 | Joe | 70000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 |+----+-------+--------+--------------+5 rows in set (0.00 sec)
或者
mysql> select * from Employee E where (select count(distinct Salary) from Employee where DepartmentId=E.DepartmentId and Salary>E.Salary)<3 order by DepartmentId,Salary desc;+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 4 | Max | 90000 | 1 || 6 | Randy | 85000 | 1 || 1 | Joe | 70000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 |+----+-------+--------+--------------+5 rows in set (0.00 sec)
0 0
- mysql自连接
- mysql 自连接
- Mysql自连接查询
- mysql 自连接
- mysql 自连接 公交换乘
- mysql 左连接 自连接 例子
- Mysql中的关联查询(内连接,外连接,自连接)
- mysql自连接与join关联
- MySQL学习----MySQL 连接和联合----16MySQL 自连接和联合
- 自连接
- 自连接
- 自连接
- 自连接
- 自连接
- 自连接
- 自连接
- 自连接
- 自连接
- 设计模式二:策略模式
- (四)CXF整合Spring发布WebService服务
- centos+nginx从零开始配置负载均衡
- 6-1-2 字符类型-逃逸字符?-\b-\t-\n-\r
- C++ 超短字符串比较
- mysql 自连接
- hiho一下 第三十周 小Hi小Ho的惊天大作战:扫雷·一
- vim配置
- 基础排序算法:冒泡法,选择法,直接插入法,归并排序法
- Android基础类之BaseAdapter
- 2015重磅炸弹——【视频】Android设计招式之美
- RedHat Linux 网络配置文件详解
- POJ 1836 Alignment 枚举中间点双向求LIS
- 打印任意一年的日历