当前位置:网站首页>MySQL数据库基础:连接查询
MySQL数据库基础:连接查询
2022-06-21 06:13:00 【袗亦】
【MySQL数据库基础:7.连接查询:sql99语法】
作者:zhenyi
专栏:mysql数据库1
简介:MySQL基础专栏文章,适合MySQL基础阶段的学习与阅读。
文章所用文件,可与作者联系获取!如果对您有帮助,点个关注吧,持续更新中,谢谢支持。如果有问题或错误,欢迎指出私信与我联系,谢谢。
目录
例3. 查询部门个数>3的城市名和部门个数,(添加分组+筛选)
例4.查询哪个部门的员工个数>3的部门名和员工个数,并按个数降序(添加排序)
例5.查询员工名、部门名、工种名,并按部门名降序(添加三表连接)
二、sql99语法
语法:
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
【where 筛选条件】
【group by 分组】
【having 筛选条件】
【order by 排序列表】
分类:
内连接:inner
外连接
左外:left 【outer】
右外:right 【outer】
全外:full【outer】
交叉连接:cross
一、内连接
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件;
分类:
等值
非等值
自连接
特点:
①添加排序、分组、筛选
②inner可以省略
③ 筛选条件放在where后面,连接条件放在on后面,提高分离性,便于阅读
④inner join连接和sql92语法中的等值连接效果是一样的,都是查询多表的交集
1、等值连接
例1.查询员工名、部门名
SELECT last_name,department_name
FROM departments as a
JOIN employees as b
ON b.department_id=a.department_id;
SELECT last_name,department_name
FROM departments as a
JOIN employees as b
ON b.department_id=a.department_id;查询结果如下:

例2.查询名字中包含e的员工名和工种名(添加筛选)
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`= j.`job_id`
WHERE e.`last_name` LIKE '%e%';
SELECT last_name,job_title
FROM employees as a
INNER JOIN jobs as b
ON a.job_id=b.job_id
WHERE a.last_name LIKE '%e%';查询结果如下:

例3. 查询部门个数>3的城市名和部门个数,(添加分组+筛选)
#①查询每个城市的部门个数
#②在①结果上筛选满足条件的
SELECT city,COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
SELECT city,COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;查询结果如下:

例4.查询哪个部门的员工个数>3的部门名和员工个数,并按个数降序(添加排序)
#①查询每个部门的员工个数
SELECT COUNT(*),department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
#② 在①结果上筛选员工个数>3的记录,并排序
SELECT COUNT(*) 个数,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
SELECT COUNT(*) 个数,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;查询结果如下:

例5.查询员工名、部门名、工种名,并按部门名降序(添加三表连接)
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.`department_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.`department_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;查询结果如下:

二、自连接
例题1:查询员工的名字、上级的名字
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`;
SELECT a.last_name,b.last_name
FROM employees as a
JOIN employees as b
ON a.manager_id=b.employee_id;查询结果如下:
例题2:查询姓名中包含字符k的员工的名字、上级的名字
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id`= m.`employee_id`
WHERE e.`last_name` LIKE '%k%';
SELECT a.last_name,b.last_name
FROM employees as a
JOIN employees as b
ON a.manager_id=b.employee_id;
WHERE e.`last_name` LIKE '%k%';查询结果如下:

边栏推荐
- 深度学习的几种优化方法
- leetcode 675. 为高尔夫比赛砍树——(每日一难day29)
- Memorizing Normality to Detect Anomaly: Memory-augmented Deep Autoencoder for Unsupervised Anomaly D
- 正则表达式基础
- FPGA - 7系列 FPGA SelectIO -01- 简介与DCI技术简介
- 如何限制内网网速
- Niuke-top101-bm25
- 【数据挖掘】期末复习 第四章
- tf. contrib. slim. conv2d()
- You have an error in your SQL syntax; check the manual that corresponds to your MYSQL server
猜你喜欢

Memorizing Normality to Detect Anomaly: Memory-augmented Deep Autoencoder for Unsupervised Anomaly D

利用burp进行爆破(普通爆破+验证码爆破)
![[[graduation season · advanced technology Er] - experience shared by senior students](/img/15/720dac05ebba3ead1b82b15f15daa6.png)
[[graduation season · advanced technology Er] - experience shared by senior students

Digital signal processing-07-dds IP application example

Backtracking method of graph coloring problem (the most easy to understand)
![[JVM] classloader](/img/32/5bf40969528285fb0d0f7f98ba585a.png)
[JVM] classloader

Aurora8B10B IP使用 -02- IP功能设计技巧

MSF内网渗透
![[is the network you are familiar with really safe?] Wanziwen](/img/b4/6092ab3fd728e5d453ec38b089d027.png)
[is the network you are familiar with really safe?] Wanziwen

Solve the first problem of Huawei's machine test on April 20 by recursion and circulation (100 points)
随机推荐
FPGA - 7系列 FPGA SelectIO -06- 逻辑资源之ODELAY
Leetcode question brushing - (4) the first unique character in the string
Which of the children's critical illness insurance companies has the highest cost performance in 2022?
Aurora8B10B IP使用 -04- IP例程应用实例
The database is 8.0 students. In this place, add this paragraph? useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8
tf. Operation
MSF内网渗透
Chapter 2: Data Model (final review of database)
Aurora8B10B IP使用 -02- IP功能設計技巧
tf. QueueBase
Does intelligence need a body
Several optimization methods of deep learning
Survey shows that data analysis is crucial to product success
DDD Practice Manual (4. aggregate aggregate)
C语言实现模拟银行存取款管理系统课程设计(纯C语言版)
对网页 ‘’为所欲为‘’ 之手动实现 csdn深色模式?
IP - 射频数据转换器 -04- API使用指南 - 系统设置相关函数
leetcode 675. 为高尔夫比赛砍树——(每日一难day29)
【【毕业季·进击的技术er】------老学长心得分享
FPGA - 7系列 FPGA SelectIO -05- 逻辑资源之OLOGIC
