当前位置:网站首页>MySQL中的多表查询
MySQL中的多表查询
2022-06-09 04:08:00 【允谦呀】
MySQL中的多表查询
多表查询的分类
- 等值连接—— 非等值连接
- 自连接 —— 非自连接
- 内连接 —— 外连接
# 等值连接 vs 非等值连接
# 非等值连接的例子
SELECT* FROM job_grades;
SELECT last_name, salary,grade_level
FROM employees e, job_grades j
WHERE e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`
# 自连接 vs 非自连接
SELECT * FROM employees;
# 自连接
# 查询员工id,员工姓名及其管理者的id和姓名
SELECT emp.employee_id, emp.last_name, mgr.employee_id ,mgr.last_name
FROM employees emp, employees mgr
WHERE emp.`manager_id` = mgr.`employee_id`
# 内连接 vs 外连接
# 内连接:合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行
SELECT employee_id, department_name
FROM employees e, departments d
WHERE e.`department_id` = d.`department_id`
# 外连接:合并具有同一列的两个以上的表的行,结果集中除了包含一个表与另一个表匹配的行之外
# 还查询到了坐标或右表不匹配的行。
# 外连接的分类:左外连接、右外连接、满外连接
# 左外连接:两个表在连接过程中除了瞒住连接条件的行以外还返回左表中不满足条件的行,这种连接称为左外连接
# 右外连接:两个表在连接过程中除了瞒住连接条件的行以外还返回右表中不满足条件的行,这种连接称为右外连接
# 查询所有员工的last_name,department_name信息
# SQL92语法实现内连接
SELECT employee_id, department_name
FROM employees e, departments d
WHERE e.`department_id` = d.`department_id`
# SQL92语法实现外连接 使用+ ————MySQL中不支持SQL92语法中的外连接的写法
SELECT employee_id, department_name
FROM employees e, departments d
WHERE e.`department_id` = d.`department_id`(+)
# SQL99语法中使用JOIN...ON的方式实现多表的查询,这种方式也能拿个解决外连接的问题,MySQL是支持此种方式的
# SQL99语法如何实现多表的查询
# SQL99语法实现内连接
SELECT last_name,department_name
FROM employees JOIN departments
ON employees.`department_id` = departments.`department_id`
# SQL99语法实现左外连接:
SELECT last_name,department_name
FROM employees LEFT JOIN departments
ON employees.`department_id` = departments.`department_id`
# SQL99语法实现右外连接:
SELECT last_name,department_name
FROM employees RIGHT JOIN departments
ON employees.`department_id` = departments.`department_id`
#满外连接:mysql不支持 FULL OUTER JOIN
SELECT last_name,department_name
FROM employees FULL OUTER JOIN departments
ON employees.`department_id` = departments.`department_id`
# SQL99语法实现满外连接:UNION [ALL] 的使用
# union:会执行去重的操作
# union all:不会执行去重的操作
UNION的使用
合并查询结果
利用UNION关键字可以给出多条SELECT语句,并将他们的结果组合成单个结果集。合并时,两个表对应的列数和数据必须相同,并且相互对应。各个SELECT语句之间使用UNION或UNION ALL关键字分隔。
语法格式:
SELECT column,... from table1
UNION [ALL]
SELECT column,... from table2
UNION操作符返回两个查询的结果的并集,取出重复记录
UNION ALL操作符返回两个查询的结果集的并集,对于两个结果集的重复的部分,不去重。
如果明确知道合并后的数据不存在重复的诗句,或者不需要去除重复的数据,则尽量使用UNION ALL语句,以提高数据查询的效率
SQL99语法中的新特性1:自然连接
SQL99在SQL92基础上提供了一些特殊语法,比如NATURAL JOIN用来表示自然连接,我们可以把自然连接理解为SQL92中的等值连接,他会帮你自动查询两张连接表中所有相同的字段,然后进行等值连接
SQL99语法中的新特性2:USING
USING指定了具体的相同的字段名称,你需要在USING的括号()中填入指定的同名字段,使用使用JSON…USING可以简化JOIN ON的等值连接
边栏推荐
- C language interview tube: talk about the functions of various string functions. I: I don't know. Interviewer: come back for an interview when the young man has learned how to do it.
- [SWPU2019]ReverseMe
- Attention OCR Chinese version mas # ter Code Running Logic
- Is it safe to open an account in Hongye futures?
- 【word】錯誤!文檔中沒有指定樣式的文字。 1
- C语言课程设计教室简单预约系统
- Online Morse code online translation and conversion tool
- [software tools] [tutorials] a useful tool for exporting CSDN blog articles to word
- App上看到就忍不住点的小红点是如何实现的?
- 音频功率放大电路(使用过的语音方案电路记录)
猜你喜欢

golang---并发runtime包

ISCC-2022-reverse-mobile-部分wp

This artifact is highly recommended. One line command will convert the web page to PDF!

Graph to document function based on pyqt5

微信小程序:(异常)Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 解决方案和分析流程(这里一定有你要的答案)

基于PyQt5完成的PDF拆分

Assembly: common commands for debug

Getting started with Maui custom drawing

Rigidbody2d rotate around a fixed point rotate

在线JSON转XML工具
随机推荐
Rigidbody2d rotate around a fixed point rotate
Rich color changes
Large factory outsourcing or self research company? How to choose a job for a tester?
ES6 symbol explanation
汇编:jmp指令
This artifact is highly recommended. One line command will convert the web page to PDF!
Matting function based on pyqt5 - program implementation
Wechat applet: (exception) expected begin_ Object but was string at line 1 column 1 path $solution and analysis process
人才缺口50万以上,平均薪资20K?网络安全,测试员的下一个风口~
Gradle 渠道包配置
Assembly: CPU structure - flag register and related instructions
[word] error! There is no text of the specified style in the document. one
1264_FreeRTOS任务的初始化以及堆栈初始化处理分析
基于PyQt5完成的抠图界面
[leetcode] day 48 - 1037 Effective boomerang
golang ---image--热力图与照片的重叠
Binary processing of opcv image
[software tools] [tutorials] a useful tool for exporting CSDN blog articles to word
Apple Announces Winner of the 2022 Apple Design Award
Dapr 1.7 UNIX domain socket he's here