单表查询,以下面这个表为例:
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000 | 403 | 3 |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
select distinct 字段1,字段2,字段3,... from 库名.表名 #distinct 去重复功能
where约束条件
group by 按照分组依据,分组查询
having 分组后的过滤条件(针对组级别的过滤)
order by 排序依据
limit n; 限制显示的条目
语法:
1.注意先切换路径use db8,找到文件、打开文件、for循环可能是多读多行才发送给服务端,这样会减少IO
select * from db8.emp
from db8.emp
2.优先级顺序
from打开文件(把表从硬盘读入内存) ——>where条件过滤——>group by分组
执行顺序——————>————————>
group by:聚合函数(avg、sum、max、min等)只能在分完组之后才能使用
如果不写,其实默认是有where、group by分组的
例如:
select max(salary) from emp;
+-------------+
| max(salary) |
+-------------+
| 1000000.31 |
+-------------+
distinct:去的是记录的重复
select distinct sex from emp; 去除筛选的重复的sex
+--------+
| sex |
+--------+
| male |
| female |
+--------+
where 过滤:
1.between..and (包含=两边的)
select id,name from emp where id between 3 and 6; # id>=3 and id<=6
not between 3 and 6 #可以取反
2.in
<select * from emp where salary =20000 or salary = 18000 or salary=17000;>
select * from emp where salary in(17000,18000,20000); #最终版(取代连续or的条件)
not in #可以取反
3.like: _代表任意单个字符 %:代表任意无穷个字符
要求:查找emp里面含有‘i’字母的员工姓名与其薪资
select name,salary from emp where name like ‘%i%‘;
要求:查询员工姓名由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like ‘____‘;
要求:查询员工职位描述post_comment为空姓名与职位: (判断NULL只能用is,不能用=)
select name,post from emp where post_comment is NULL;
is not NULL #取反
group by分组: 聚合函数(avg、sum、max、min等)只能在分完组之后才能使用
什么是分组:找重复度高的,即有大量重复的字段去使用(id、name重复度太低,不能使用group by)
为何要分组:当我们要以组为单位进行统计时就必须分组
补充:#设置sql_mode为only_full_group_by,一位着以后分组,只能取分组的依据
查看sql_mode:
1.show variables like ‘sql_mode‘;
+---------------+---------------------+
| Variable_name | Value |
+---------------+---------------------+
| sql_mode | STRICT_TRANS_TABLES |
+---------------+---------------------+
2.set global sql_mode=‘strict_trans_tables,only_full_group_by‘ ; 这个严格模式设置完之后以后就只能取组名,单 单独分组后的内容是看不到的
即只能取:select post from emp group by post; (name,id。。等都取不到),说明分组是为了整体来用,把数据聚集在一起,不是为 了单独取某条数据
3.select @@sql_mode #查看是否更改成功
1.max求每个部门最高工资(select * from emp group by post;分完组之后默认*取出的是每个组的第一个人)
分组目的是以组为单位做一个整体的聚合,拿到聚合之后的结果
select post,max(salary) from emp group by post;
取每个部门最低工资:
select post,min(salary) from emp group by post;
取部门的平均工资:
select post,avg(salary) from emp group by post;
取部门的工资总和:
select post,sum (salary) from emp group by post;
统计每个部门个数:(按照id)
select post,count(id) from emp group by post;
having by:过滤条件,可以用聚合函数,因为是在分组之后
where分组之前做的过滤
having 分组之后专门对聚合的结果做进一步筛选
select age from emp group by age having age > 28;
group_concat字符串拼接:
查看所有部门和对应的员工名字:
select post,group_concat(name) from emp group by post;
+----------------------------+-------------------------------------------------------+
| post | group_concat(name) |
+----------------------------+-------------------------------------------------------+
| operation | 程咬铁,程咬铜,程咬银,程咬金,张野 |
| sale | 格格,星星,丁丁,丫丫,歪歪 |
| teacher | 成龙,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex |
| 老男孩驻沙河办事处外交大使 | egon |
+----------------------------+-------------------------------------------------------+
select post,group_concat(name,‘_perfect‘) from emp group by post; #实质就是一个字符串的凭借
select post,group_concat(salary) from emp group by post;
select post,group_concat(name,‘:‘,salary) from emp group by post;
练习:
1.查询岗位名以及包含的所有员工的名字:
select post,group_concat(name) from emp group by post;
2.查询岗位名已经岗位内包含的员工个数
select post,count(id) from emp group by post;
3.所有男员工、女员工的个数:
select sex,count(id) from emp group by sex;
4.取部门的平均工资:
select post,avg(salary) from emp group by post;
5.查询男员工与女员工的平均薪资
select sex,avg(salary) from emp group by sex;
6.统计各部门年龄在30岁以上的员工平均工资:(分析表的结果包含:部门、平均薪资)
select post,avg(salary) from emp where age >= 30 group by post;
+---------+---------------+
| post | avg(salary) |
+---------+---------------+
| sale | 2500.240000 |
| teacher | 255450.077500 |
+---------+---------------+
补充concat不分组时用
select name as 姓名,salary as 薪资 from emp;
select concat(‘Name:‘,name) as 姓名,concat(‘Sal:‘,salary) as 薪资 from emp;
+-----------------+----------------+
| 姓名 | 薪资 |
+-----------------+----------------+
| Name:egon | Sal:7300.33 |
| Name:alex | Sal:1000000.31 |
| Name:wupeiqi | Sal:8300.00 |
| Name:yuanhao | Sal:3500.00 |
| Name:liwenzhou | Sal:2100.00 |
| Name:jingliyang | Sal:9000.00 |
| Name:jinxin | Sal:30000.00 |
| Name:成龙 | Sal:10000.00 |
| Name:歪歪 | Sal:3000.13 |
| Name:丫丫 | Sal:2000.35 |
| Name:丁丁 | Sal:1000.37 |
| Name:星星 | Sal:3000.29 |
| Name:格格 | Sal:4000.33 |
| Name:张野 | Sal:10000.13 |
| Name:程咬金 | Sal:20000.00 |
| Name:程咬银 | Sal:19000.00 |
| Name:程咬铜 | Sal:18000.00 |
| Name:程咬铁 | Sal:17000.00 |
+-----------------+----------------+
select concat(name,‘:‘,age,‘:‘,sex) from emp; #正常情况每个字段间都要输入:
select concat_ws(‘:‘,name,age,sex) from emp; #concat_ws就是需要在多个表中间要加‘:’时,只需要在最前面加‘:’就自动会在多个字段加
mysql> select concat_ws(‘:‘,name,age,
sex) from emp;
+-----------------------------+
| concat_ws(‘:‘,name,age,sex) |
+-----------------------------+
| egon:18:male |
| alex:78:male |
| wupeiqi:81:male |
| yuanhao:73:male |
| liwenzhou:28:male |
| jingliyang:18:female |
| jinxin:18:male |
| 成龙:48:male |
| 歪歪:48:female |
| 丫丫:38:female |
| 丁丁:18:female |
| 星星:18:female |
| 格格:28:female |
| 张野:28:male |
| 程咬金:18:male |
| 程咬银:18:female |
| 程咬铜:18:male |
| 程咬铁:18:female |
+-----------------------------+
四则运算:
计算年薪:
select name,salary*12 as annual_salary from emp; #默认可以不写as
+------------+---------------+
| name | annual_salary |
+------------+---------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+---------------+
select * from emp; #正常情况
select * from emp as t1; #正常情况
select empoyee.* from emp as empoyee; #如果要是表名.* 后面必须就要一致,说明 as empoyee先运行
order by排序:
升序:
select age from emp order by age; #select age from emp order by age asc; 默认是升序后面有asc
降序:
select age from emp order by age desc;
limit限制显示的条件:
select * from emp limit 4; #打印只会显示前面4条信息
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
limit分页显示:
select * from emp limit 0,5; #从0开始(不包含0)往后取5条,会打印1-5
select * from emp limit 5,5; #从5开始往后取5条,会打印6-10
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
正则表达式:
select * from emp where name regexp ‘^jin.*(g|n)$‘; #jin开头,.*中间可以是任意字符,g/n结尾的名字
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
多表查询:
emp:
+-----+----------+ +----+--------
| id | name | sex | age | dep_id |
+----+------------+--------+-----+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+-----+--------+ +-----+----------+
dep:
| id | name |
+-----+----------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
select * from emp,dep 同时查询2张表:(本质是一份数据,员工部门表)
原理:左边的一天记录会完整的把右边的记录对应一遍:
+----+------------+--------+-----+--------+-----+----------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+-----+--------+-----+----------+
| 1 | egon | male | 18 | 200 | 200 | 技术 | egon完整与部门表对应一次
| 1 | egon | male | 18 | 200 | 201 | 人力资源 |
| 1 | egon | male | 18 | 200 | 202 | 销售 |
| 1 | egon | male | 18 | 200 | 203 | 运营 |
| 2 | alex | female | 48 | 201 | 200 | 技术 | alex完整与部门表对应一次
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 2 | alex | female | 48 | 201 | 202 | 销售 |
| 2 | alex | female | 48 | 201 | 203 | 运营 |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技术 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 运营 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技术 |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 运营 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 201 | 人力资源 |
| 5 | liwenzhou | male | 18 | 200 | 202 | 销售 |
| 5 | liwenzhou | male | 18 | 200 | 203 | 运营 |
| 6 | jingliyang | female | 18 | 204 | 200 | 技术 |
| 6 | jingliyang | female | 18 | 204 | 201 | 人力资源 |
| 6 | jingliyang | female | 18 | 204 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | 203 | 运营 |
+----+------------+--------+-----+--------+-----+----------+
笛卡尔积
汇总员工表emp里面对应的部门信息:
select * from emp,dep where emp.dep_id = dep.id; (基础写法)
+-----+----------+----+-----------+--------+-----+--------+
| id | name | id | name | sex | age | dep_id |
+-----+----------+----+-----------+--------+-----+--------+
| 200 | 技术 | 1 | egon | male | 18 | 200 |
| 201 | 人力资源 | 2 | alex | female | 48 | 201 |
| 201 | 人力资源 | 3 | wupeiqi | male | 38 | 201 |
| 202 | 销售 | 4 | yuanhao | female | 28 | 202 |
| 200 | 技术 | 5 | liwenzhou | male | 18 | 200 |
+-----+----------+----+-----------+--------+-----+--------+
只打印技术部门的人:
select * from dep,emp where emp.dep_id = dep.id and dep.name=‘技术‘;(在基础上再加and的判断语句)
+-----+------+----+-----------+------+-----+--------+
| id | name | id | name | sex | age | dep_id |
+-----+------+----+-----------+------+-----+--------+
| 200 | 技术 | 1 | egon | male | 18 | 200 |
| 200 | 技术 | 5 | liwenzhou | male | 18 | 200 |
+-----+------+----+-----------+------+-----+--------+
只查技术部的人名:
select emp.name from emp,dep where emp.dep_id = dep.id and dep.name=‘技术‘;
3.1、2条件基础上对结果近一步筛选 1.笛卡尔积筛选后的基础表格 2.基于基础添加判断进一步筛选
+-----------+
| name |
+-----------+
| egon |
| liwenzhou |
+-----------+
查egon所在的部门名:
select dep.name from emp,dep where emp.dep_id = dep.id and emp.name=‘egon‘;
+------+
| name |
+------+
| 技术 |
+------+
1.内连接:不推荐
select * from emp,dep where emp.dep_id = dep.id;
2.内连接标准写法:inner join...on:只取两张表有对应关系的记录
inner join...on 只取相同部分,与上面原理一样
select * from emp inner join dep on emp.dep_id = dep.id where dep.name=‘技术‘;
思考:emp里面没有对应关系的 jingliyang 204没出来,如何做保留
左连接:在内连接的基础上,保留左表没有对应关系的记录
select * from emp left joindep on emp.dep_id = dep.id;
+----+------------+--------+-----+--------+------+----------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+-----+--------+------+----------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
+----+------------+--------+-----+--------+------+----------+
6 rows in set
右连接:在内连接的基础上,保留右表没有对应关系的记录
select * from emp right join dep on emp.dep_id = dep.id;
+------+-----------+--------+------+--------+-----+----------+
| id | name | sex | age | dep_id | id | name |
+------+-----------+--------+------+--------+-----+----------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+-----------+--------+------+--------+-----+----------+
全连接:在内连接的基础上保留右表没有对应关系的记录
select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;
+------+------------+--------+------+--------+------+----------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+----------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+------------+--------+------+--------+------+----------+
工作顺序就是:
1.先实现笛卡尔积
2.再看是数据inner join、left join、 right join
虚拟表连接查询:
环境是只有一个emp员工信息工资表:
t1:表格
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000 | 403 | 3 |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
t2表格:(虚拟表)
+----------------------------+-------------+
| post | max(salary) |
+----------------------------+-------------+
| operation | 20000 |
| sale | 4000.33 |
| teacher | 1000000.31 |
| 老男孩驻沙河办事处外交大使 | 7300.33 |
+----------------------------+-------------+
实例2:
1.思考怎么实现找到薪资最高的人,首先不能直接在emp这张表上直接实现得到
2.想得到每个部门薪资最高的人,其实就是在t2的基础上做近一步的筛选
3.为了拿到人名,所有将t1与t2合并,从新表里再取提取对应的薪资最高的人
将2张表格 inner join
select t1.id,t1.name,t1.salary,t1.post,t2.post,t2.ms
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2 #将t2的max(salary)字段变为ms ;as t2是将整个t2表格变为t2
on t1.post = t2.post;
+----+------------+------------+----------------------------+----------------------------+------------+
| id | name | salary | post | post | ms |
+----+------------+------------+----------------------------+----------------------------+------------+
| 1 | egon | 7300.33 | 老男孩驻沙河办事处外交大使 | 老男孩驻沙河办事处外交大使 | 7300.33 |
| 2 | alex | 1000000.31 | teacher | teacher | 1000000.31 |
| 3 | wupeiqi | 8300 | teacher | teacher | 1000000.31 | #前面的名字是数据后面部门的,后面的薪资是这个部门的最高工资
| 4 | yuanhao | 3500 | teacher | teacher | 1000000.31 |
| 5 | liwenzhou | 2100 | teacher | teacher | 1000000.31 |
| 6 | jingliyang | 9000 | teacher | teacher | 1000000.31 |
| 7 | jinxin | 30000 | teacher | teacher | 1000000.31 |
| 8 | 成龙 | 10000 | teacher | teacher | 1000000.31 |
| 9 | 歪歪 | 3000.13 | sale | sale | 4000.33 |
| 10 | 丫丫 | 2000.35 | sale | sale | 4000.33 |
| 11 | 丁丁 | 1000.37 | sale | sale | 4000.33 |
| 12 | 星星 | 3000.29 | sale | sale | 4000.33 |
| 13 | 格格 | 4000.33 | sale | sale | 4000.33 |
| 14 | 张野 | 10000.13 | operation | operation | 20000 |
| 15 | 程咬金 | 20000 | operation | operation | 20000 |
| 16 | 程咬银 | 19000 | operation | operation | 20000 |
| 17 | 程咬铜 | 18000 | operation | operation | 20000 |
| 18 | 程咬铁 | 17000 | operation | operation | 20000 |
+----+------------+------------+----------------------------+----------------------------+------------+
18 rows in set
连表的目的是:判断员工薪资是不是部门最高工资 t1.salary = t2.ms
select t1.id,t1.name,t1.salary,t1.post,t2.post,t2.ms
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary=t2.ms;
+----+--------+------------+----------------------------+----------------------------+------------+
| id | name | salary | post | post | ms |
+----+--------+------------+----------------------------+----------------------------+------------+
| 1 | egon | 7300.33 | 老男孩驻沙河办事处外交大使 | 老男孩驻沙河办事处外交大使 | 7300.33 |
| 2 | alex | 1000000.31 | teacher | teacher | 1000000.31 |
| 13 | 格格 | 4000.33 | sale | sale | 4000.33 |
| 15 | 程咬金 | 20000 | operation | operation | 20000 |
+----+--------+------------+----------------------------+----------------------------+------------+
最终版:代码实现拿到薪资最高对应的人
select t1.* #最后要的只是t1表里面的信息,唯一就是筛选出了最高工资对应的人,(t2实质就是中间过度判断的依据)
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary=t2.ms;
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
原文地址:https://www.cnblogs.com/yangzhizong/p/9356894.html