服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL多表查询详解上

MySQL多表查询详解上

2021-01-23 19:34木卯生十木 Mysql

这篇文章主要介绍了MySQL多表查询详解上,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

时光在不经意间,总是过得出奇的快。小暑已过,进入中暑,太阳更加热烈的绽放着ta的光芒,...在外面被太阳照顾的人们啊,你们都是勤劳与可爱的人啊。在房子里已各种姿势看我这篇这章的你,既然点了进来,那就由我继续带你回顾mysql的知识吧!

回顾练习资料girls库以及两张表的脚本:

链接: https://pan.baidu.com/s/1bgfrp7dbbwk3ao755pu4qg 提取码: ihg7

引题:笛卡尔现象,先来观看一下两张表。

?
1
select * from boys;

MySQL多表查询详解上

?
1
select * from beauty;

MySQL多表查询详解上

?
1
2
select name,boyname from boys,beauty;
最终结果:12*4=48行

MySQL多表查询详解上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#进阶6:连接查询
含义:又称多表查询,当查询的字段来自于多个表时,就会用到连接查询
 
笛卡儿积现象: 表1 有m行,表2有n行,结果=m*n行
产生原因:没有有效的连接条件
解决方法:添加有效的连接条件
 连接分类:
   按年代分类:
   sql1992标准(192标准):仅支持内连接
   sql1999标准(199标准) [推荐]:支持内连接+外联结(左外与右外)+交叉连接
 
  按功能分类:
   内连接:
     等值连接
     非等值连接
     自联结
   外连接:
     左外连接
     右外连接
     全外连接
    交叉连接:
      左外连接
      右外连接
      全外连接
   交叉连接:
?
1
2
select name,boyname from boys,beauty
where beauty.boyfriend_id = boys.id;

MySQL多表查询详解上

?
1
2
3
4
5
6
7
8
9
10
11
12
#一.sql192标准
#1.等值连接
 ①多表等值连接的结果为多表的交集部分
 ②n表连接,至少需要n-1个连接条件
 ③多表的顺序没有要求
 ④一般需要为表起别名
 ⑤可以搭配前面介绍的所有子句使用,比如,排序,分组,筛选。
#多表查询,先匹配在筛选
#案例1.查询员工名和对应的部门名。
select first_name as 名,department_name as 部门名
from employees,departments
where employees.department_id = departments.department_id;

MySQL多表查询详解上

?
1
2
3
4
5
#案例2.查询员工名,工种号,工种名。对于两张表共有的字段需要加表名作限定不然会报错。
错误示例:
select first_name as 名,employees.job_id as 工种号,job_title as 工种名
from employees,jobs
where employees.job_id = jobs.job_id;
?
1
2
3
4
5
6
7
#2.为表起别名
 ①提高语句的简洁度
 ②区分多个重命名的字段
注意:如果为表起了别名,则查询的字段就不能使用原来的表名去限定
select first_name as 名,e.job_id as 工种号,job_title as 工种名
from employees as e,jobs as j
where e.job_id = j.job_id;

MySQL多表查询详解上

给表起了别名,再用表完整名子做限定会报错,不允许。根据执行顺序走先走from,
走完from后就用别名的,相当于生成了一个虚拟的视图,不再认原来的表名。

MySQL多表查询详解上

?
1
2
3
4
5
6
7
8
9
10
11
#3.两个表名的顺序是否可以调换,是可以调换的。
select first_name as 名,e.job_id as 工种号,job_title as 工种名
from jobs as j,employees as e
where e.job_id = j.job_id;
 
#4.可以加筛选
#案例3.查询有奖金的员工名,部门名。
select first_name as 名,department_name as 部门名,commission_pct as 奖金
from employees as e,departments as d
where e.department_id=d.department_id
and commission_pct is not null;#and e.commission_pct is not null;

MySQL多表查询详解上

?
1
2
3
4
5
#案例4.查询城市名中第二个字符为o对应的城市名与部门名。
select city as 城市,department_name as 部门名
from locations as l,departments as d
where l.location_id = d.location_id
and city like '_o%';

MySQL多表查询详解上

?
1
2
3
4
5
6
#5.可以加分组
#案例1.查询每个城市的部门个数。
select city as 城市,count(department_id) as 个数
from locations as l,departments as d
where l.location_id = d.location_id
group by l.city;

MySQL多表查询详解上

?
1
2
3
4
5
6
7
8
#案例2.查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资。
#查询的时候不确定把两个列都加上。
select commission_pct as 奖金,department_name as 部门名,
d.manager_id as 领导编号,min(salary) as 最低工资
from employees as e,departments as d
where e.department_id = d.department_id
and commission_pct is not null
group by department_name,d.manager_id;

MySQL多表查询详解上

?
1
2
3
4
5
6
7
#6.可以加排序
#案例1:查询每个工种的工种名和员工的个数,并且按员工个数降序。
select j.job_title as 工种名,count(employee_id) as 个数
from employees as e,jobs as j
where e.job_id = j.job_id
group by job_title
order by 个数 desc;

MySQL多表查询详解上

?
1
2
3
4
5
6
#7.三表连接
#案例1.查询员工名,部门名与所在的城市
select first_name as 名,d.manager_id as 部门名,city as 城市
from employees as e,departments as d,locations as l
where e.department_id = d.department_id
 and d.location_id = l.location_id;

MySQL多表查询详解上

?
1
2
3
4
5
6
#案例2.查询员工名,部门名与所在的城市,城市以s开头。
select first_name as 名,d.manager_id as 部门名,city as 城市
from employees as e,departments as d,locations as l
where e.department_id = d.department_id
and d.location_id = l.location_id
 and city like 's%';

MySQL多表查询详解上

?
1
2
3
4
5
6
7
#案例3.查询员工名,部门名与所在的城市,城市以s开头,按姓名降序排列。
select first_name as 名,d.manager_id as 部门名,city as 城市
from employees as e,departments as d,locations as l
where e.department_id = d.department_id
and d.location_id = l.location_id
and city like 's%'
order by department_name desc;

MySQL多表查询详解上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
新增一张工资等级表。
create table job_grades
(grade_level varchar(3),
 lowest_sal int,
 highest_sal int);
insert into job_grades
values ('a', 1000, 2999);
insert into job_grades
values ('b', 3000, 5999);
insert into job_grades
values('c', 6000, 9999);
insert into job_grades
values('d', 10000, 14999);
insert into job_grades
values('e', 15000, 24999);
insert into job_grades
values('f', 25000, 40000);

MySQL多表查询详解上

?
1
2
3
4
5
#2.非等值连接,(范围判断)
#案例1.查询员工的工资和工资级别。
select salary as 工资,grade_level as 等级
from employees as e,job_grades as g
where salary between g.lowest_sal and g.highest_sal;

MySQL多表查询详解上

?
1
2
3
4
5
#案例2.查询员工的工资和工资级别,展示出a级别的员工。
select salary as 工资,grade_level as 等级
from employees as e,job_grades as g
where salary between g.lowest_sal and g.highest_sal
and g.grade_level = 'a';

MySQL多表查询详解上

?
1
#3.自联接[自己连接自己]

MySQL多表查询详解上

?
1
2
3
4
5
#案例1.查询员工名和上级的名称.
select e.employee_id,e.last_name as 员工,
m.employee_id,m.last_name as 领导
from employees e,employees m
where e.manager_id = m.employee_id;

MySQL多表查询详解上

经过了以上的示例,相信你对多表查询已经有了一个了解,赶快动动你的小手手来练习一下吧!o(^▽^)o

到此这篇关于mysql多表查询详解上的文章就介绍到这了,更多相关mysql多表查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/jxearlier/p/13375930.html

延伸 · 阅读

精彩推荐