首页 > 数据库 >【MySQL】多表查询7种SQL JOIN的实现

【MySQL】多表查询7种SQL JOIN的实现

时间:2022-08-18 18:56:26浏览次数:63  
标签:JOIN name employee MySQL department 多表 id SELECT

目录

两个表关联有这7种情况

image

1.内连接

image

SELECT employee_id,department_name
FROM employees e JOIN departments d
ON e.`department_id` = d.`department_id`;

2.左外连接

image

SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.`department_id` = d.`department_id`;

3.右外连接

image

SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`;

4.A-A ∩ B

image

SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE d.`department_id` IS NULL;

5.B-A ∩ B

image

SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE e.`department_id` IS NULL;

6.满外连接

image
两种方式取并集
image

方式一:

SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.`department_id` = d.`department_id`
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE e.`department_id` IS NULL;

方式二:

SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE d.`department_id` IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`;

7.去除交集部分

image
这两个取并集
image

SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE d.`department_id` IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE e.`department_id` IS NULL;

标签:JOIN,name,employee,MySQL,department,多表,id,SELECT
From: https://www.cnblogs.com/zhishu/p/16596451.html

相关文章

  • 【MySQL】合并查询结果UNION
    1.UNION合并多条SELECT语句的查询结果,两个表对应的列数和数据类型必须相同,并且相互对应。会执行去重操作。2.UNIONALL不会执行去重操作如果明确知道合并数据后的结......
  • 【MySQL】多表查询:内连接VS外连接
    区别:内连接只取两张表中满足where条件的部分(交集部分),外连接除了取满足的部分还会取不满足的部分。因此外连接又可以分为:左外连接、右外连接、全外连接。1.内连接SQL92内......
  • canal同步mysql实战
    环境mysql5.6.41canal1.151.16测试过后,一直报错canal_config表不存在,更换版本后正常目的:同步一个数据库中的二个表1、创建表CREATETABLE`user01`(`id`int(......
  • CentOS7安装mysql5.7
    非原创,用到了,所以在这里记录一下。摘自:centos7安装mysql5.7步骤(图解版)_小志的博客的博客-CSDN博客_centos7安装mysql5.7目录一、下载mysql5.7安装包二、mysql5.7......
  • mysql varchar
    转载1: https://www.cnblogs.com/ryuma/p/15181704.html数据库中gbk编码一个字符占用2个字节,utf8编码一个字符占3个字节,utf8mb4编码一个字符占4个字节 转载2:https......
  • 本地navicat链接宝塔服务器的mysql
    1、如果提示hostxxxxnotallowedmysqlserver 处理方式:去my.ini文件添加一下bind-adress=0.0.0.0操作完以后可以重启mysql,在本地的cmd中telnet ip 端口 是不是......
  • join方法
    publicstaticStringjoin(CharSequencedelimiter,CharSequence...elements)返回由CharSequenceelements的副本组成的新String,该副本与指定的delimiter的副本连接......
  • mysql执行语句卡死后杀线程
    1.查询正在运行的线程SHOWPROCESSLIST;#或select*frominformation_schema.PROCESSLIST;2.批量生成杀线程的命令selectconcat("kill",ID,";")ascommand......
  • mysql 报错 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting tran
    产生这个问题的原因是因为在mysql中产生了事务A,执行了修改的语句,比如:updatet1setaget=18whereid=1;此时事务并未进行提交,事务B开始运行,也同样需要修改id为1的用户的......
  • 浮点数 mysql golang 时间序列
     1.6607259e+091660725877mysql>SELECTVal,CreateTs,CreateTsFROMTabWHEREDeviceId=156ANDOID=".1.3.6.1.4.1.28713.1.2.2.0" ANDCreateTs>=1660704714AN......