当前位置:网站首页>SQL学习笔记九种连接2
SQL学习笔记九种连接2
2022-07-01 06:21:00 【小美元】
等值连接 vs 非等值连接
SELECT last_name,salary ,job_grades.grade_level from employees,job_grades
WHERE employees.salary >= job_grades.lowest_salAND employees.salary < job_grades.highest_sal;
外连接,内连接
左外连接,右外连接
-sql 92语法实现外连接
SELECT a.first_name,a.employee_id,a.department_id FROM employees a ,departments b
WHERE a.department_id = b.department_id(+);
-mysql不支持sql92语法-
-99语法左外连接
SELECT a.first_name,a.employee_id,a.department_id FROM employees a LEFT OUTER JOIN departments b
ON a.department_id = b.department_id;
outer可以省略掉
左外连接查出来的是左边的一整张图
右外连接是右边一整张图
union
由于sql中full join不能用到,因此使用union取两个表的并集
- union all 两个图a图和b图进行合并,重复部分多余有一份,虽然冗余,但是开发中尽量用union all

中图:内连接
SELECT a.first_name,a.employee_id,a.department_id FROM employees a JOIN departments b
ON a.department_id = b.department_id;
左上图
SELECT a.first_name,a.employee_id,a.department_id FROM employees a LEFT JOIN departments b
ON a.department_id = b.department_id;
右上图
SELECT a.first_name,a.employee_id,a.department_id FROM employees a RIGHT JOIN departments b
ON a.department_id = b.department_id;
左中图
SELECT a.first_name,a.employee_id,a.department_id FROM employees a LEFT JOIN departments b
ON a.department_id = b.department_id
WHERE a.department_id = NULL;
右中图
SELECT a.first_name,a.employee_id,a.department_id FROM employees a RIGHT JOIN departments b
ON a.department_id = b.department_id
WHERE b.deparment_id = NULL;
满外连接
左上图 UNION ALL 右中图
SELECT a.first_name,a.employee_id,a.department_id FROM employees a LEFT JOIN departments b
ON a.department_id = b.department_id;
UNION ALL
SELECT a.first_name,a.employee_id,a.department_id FROM employees a RIGHT JOIN departments b
ON a.department_id = b.department_id
WHERE b.deparment_id = NULL;
边栏推荐
猜你喜欢

async 与 await

sci-hub如何使用

手把手教你实现一个深度学习框架...

Promise

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用

Design of sales management system for C language course (big homework)
![[network security tool] what is the use of USB control software](/img/cc/20fc1f35c139c52c5922727368b835.png)
[network security tool] what is the use of USB control software

SystemVerilog learning-08-random constraints and thread control

B-tree series

【企业数据安全】升级备份策略 保障企业数据安全
随机推荐
JDBC database operation
Async and await
SystemVerilog learning-07-class inheritance and package use
C语言课设工资管理系统(大作业)
Kubedm builds kubenetes cluster (Personal Learning version)
Tidb database characteristics summary
SystemVerilog learning-10-validation quantification and coverage
网络爬虫
【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
【ManageEngine卓豪】局域网监控的作用
C语言课设学生信息管理系统(大作业)
Restframework-simplejwt rewrite authentication mechanism
[ManageEngine Zhuohao] what is network operation and maintenance management and what is the use of network operation and maintenance platform
【自动化运维】自动化运维平台有什么用
10 golang operator
Diffusion (multi-source search)
码力十足学量化|如何在财务报告寻找合适的财务公告
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
Code power is full of quantitative learning | how to find a suitable financial announcement in the financial report