单表查询、多表查询、虚拟表连接查询

  单表查询,以下面这个表为例:
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 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

时间: 2024-10-23 15:48:53

单表查询、多表查询、虚拟表连接查询的相关文章

18 12 06 sql 的 基本语句 查询 条件查询 逻辑运算符 模糊查询 范围查询 排序 聚合函数 分组 分页 连接查询 自关联 子查询

-- 数据的准备 -- 创建一个数据库 create database python_test charset=utf8; -- 使用一个数据库 use python_test; -- 显示使用的当前数据是哪个? select database(); -- 创建一个数据表 -- students表 create table students( id int unsigned primary key auto_increment not null, name varchar(20) default

Mysql 表连接查询

1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和courses表中学生标识号相同的所有行.       2.外联接.外联接可以是左向外联接.右向外联接或完整外部联接.     在 FROM子句中指定外联接时,可以由下列几组关键字中的一组指定:     1)LEFT  JOIN或LEFT OUTER JOIN     左向外联接的结果集包括  LEF

Mysql表连接查询

原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和courses表中学生标识号相同的所有行. 2.外联接.外联接可以是左向外联接.右向外联接或完整外部联接. 在 FROM子句中指定外联接时,可以由下列几组关键字中的一组指定: 1)LEFT JOIN或L

数据库多表连接查询的实现方式

数据库多表连接查询的实现方式 连接查询是关系数据库中最主要的查询,主要包括自连接.内连接.外连接和交叉连接.通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中.当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息.连接操作给用户带来很大的灵活性,他们可以在任何时候增加新的数据类型.为不同实体创建新的表,然后通过连接进行查询. 内连

MySQL多表查询 三表查询 连接查询的套路

多表查询 * 当我们的一条记录 分散不同的表中时,就需要进行多表查询 例如 一对一 一对多 多对多 1.笛卡尔积查询 意思是将两个表中的所有数据 全部关联在一起   例如 a表 有2条 b表有3条   一共6条   会产生大量的错误数据 需要用添加来过滤select *from 表1,表2,....... where 过滤条件?连接查询内连接查询 inner jon   select *from 表1 join 表2 on 关系过滤条件   两边的数据必须完全匹配成功才显示     select

在数据库开发中何为虚拟表

在数据库中我们建立的表是真实存在的,比如Student表;而在查询一张或多张表时,尤其是在用可视化工具查询出来的用表显示的数据就是虚拟表;因为这个表不是像Student表那样真实存在数据库的,而是因为条件形成的我们看到的类似的表,这个表是没有名字的,但是我们仍然称它为表,而这个表就是虚拟的表了;而且这个虚拟表存在只是临时的,它不像Student表一样是永久性的(相对来说);所以在学习数据库中如果听到虚拟表之类的也就不要再有疑惑了;其实在程序开发中如果听到虚拟,就应该知道它和某种虚拟的东西类似的,

【大数据】SparkSql连接查询中的谓词下推处理(一)

本文首发于 vivo互联网技术 微信公众号 作者:李勇 目录: 1.SparkSql 2.连接查询和连接条件 3.谓词下推 4.内连接查询中的谓词下推规则 4.1.Join后条件通过AND连接 4.2.Join后条件通过OR连接 4.3.分区表使用OR连接过滤条件 1.SparkSql SparkSql 是架构在 Spark 计算框架之上的分布式 Sql 引擎,使用 DataFrame 和 DataSet 承载结构化和半结构化数据来实现数据复杂查询处理,提供的 DSL可以直接使用 scala 语

7-09内连接查询

多表连接查询:实际上是通过各个表之间共同列的关联行来查询数据的,它是关系数据库查询最主要的特征. 连接查询的分类: 内连接:根据表中共同的列进行匹配. 内连接的特点: 两个表存在主外键关系 参与查询两个表的地位相同无主次之分. 内连接查询就是两个表的数据放到一起相同的数据就是内连接查询. 内连接的实现方式: 1:使用WHERE语句指定连接条件. 2:在FROM语句中使用INNER JOIN..ON. 查询订单编号,还有商品编号: SELECT OrderInfo.OrderId,UserInfo

SQL Server中的连接查询

在查询多个表时,我们经常会用“连接查询”.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 什么是连接查询呢? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 知道了连接查询的概念之后,什么时候用连接查询呢? 一般是用作关联两张或两张以上的数据表时用的.看起来有点抽象,我们举个例子,做两张表:学生表(T_student)和班级表(T_class). T_student                            

mysql_07_连接查询

#创建数据库CREATE DATABASE db_book; #使用数据库USE `db_book`; #表如果存在则删除DROP TABLE IF EXISTS `t_book`; #创建表CREATE TABLE `t_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `bookName` varchar(20) DEFAULT NULL, `price` decimal(6,2) DEFAULT NULL, `author` varchar(20)