发布时间:2022-12-19 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

SQL 中各种连接 JOIN

数据模拟

mysql版本

select version();

SQL 中各种连接 JOIN

sql语句

CREATE TABLE `bus_sche` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `lastStation` varchar(100) NOT NULL,
  `nextStation` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO test.bus_sche (lastStation,nextStation) VALUES
	 ('武汉','南昌'),
	 ('武汉','广西'),
	 ('南昌','厦门'),
	 ('广西','上海');
CREATE TABLE `Table_A` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO test.Table_A (name) VALUES
	 ('A1'),
	 ('A3'),
	 ('A4'),
	 ('A8');
CREATE TABLE `Table_B` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `names` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO test.Table_B (names) VALUES
	 ('B1'),
	 ('B2'),
	 ('B3'),
	 ('B5'),
	 ('B6');

自连接

SELECT b.lastStation,b.nextStation,a.lastStation,a.nextStation
FROM bus_sche a, bus_sche b 
WHERE b.nextStation = a.lastStation;

SQL 中各种连接 JOIN

内连接

SQL 中各种连接 JOINSQL 中各种连接 JOIN

SELECT * from Table_A JOIN Table_B;
SELECT * from Table_A INNER JOIN Table_B;

SQL 中各种连接 JOIN

等值连接

SELECT * from Table_A A JOIN Table_B B ON A.id = B.id;

SQL 中各种连接 JOIN

SQL 中各种连接 JOIN

不等连接

SELECT * from Table_A A JOIN Table_B B ON A.id < B.id;

SQL 中各种连接 JOIN

自然连接

SELECT * from Table_A NATURAL JOIN Table_B ;
SELECT * from Table_A A NATURAL JOIN Table_B B WHERE A.id = B.id;

SQL 中各种连接 JOIN

SQL 中各种连接 JOIN

交叉连接

SQL 中各种连接 JOIN

SELECT * from Table_A CROSS JOIN Table_B;

SQL 中各种连接 JOIN

外连接

左连接(左外连接)

SELECT * from Table_A A LEFT JOIN Table_B B ON A.id = B.id;
SELECT * from Table_A A LEFT OUTER JOIN Table_B B ON A.id = B.id;

SQL 中各种连接 JOIN

SQL 中各种连接 JOIN

左表唯一

SELECT * from Table_A A LEFT JOIN Table_B B ON A.id = B.id where B.id is null

SQL 中各种连接 JOIN
SQL 中各种连接 JOIN

右连接(右外连接)

SELECT * from Table_A A RIGHT JOIN Table_B B ON A.id=B.id;
SELECT * from Table_A A RIGHT OUTER JOIN Table_B B ON A.id=B.id;

SQL 中各种连接 JOIN

SQL 中各种连接 JOIN

右表唯一

SELECT * from Table_A A RIGHT OUTER JOIN Table_B B ON A.id=B.id
where A.id is null

SQL 中各种连接 JOIN
SQL 中各种连接 JOIN

多表链接语句语法

 -- 连接两个数据表的用法:
 FROM t1 INNER JOIN t2 ON t1.id=t2.id
 -- 语法格式可以概括为:
 FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号
 -- 连接三个数据表的用法:
 FROM (t1 INNER JOIN t2 ON t1.id=t2.id) INNER JOIN t3 ON t1.name=t3.name
 -- 语法格式可以概括为:
 FROM (表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号
 -- 连接四个数据表的用法:
 FROM ((t1 INNER JOIN t2 ON t1.id=t2.id) INNER JOIN t3 ON t1.name=t3.name) INNER JOIN t4 ON t1.city=t4.city
 -- 语法格式可以概括为:
 FROM ((表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号) INNER JOIN 表4 ON 表1.字段号=表4.字段号
 -- 连接五个数据表的用法:
 FROM (((t1 INNER JOIN t2 ON t1.id=t2.id) INNER JOIN t3 ON t1.name=t3.name) INNER JOIN t4 ON t1.city=t4.city) INNER JOIN t5 ON t1.country=t5.country
 -- 语法格式可以概括为:
 FROM (((表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号) INNER JOIN 表4 ON 表1.字段号=表4.字段号) INNER JOIN 表5 ON 表1.字段号=表5.字段号

全链接(全外连接)

SQL Server版本

SQL 中各种连接 JOIN

SELECT * FROM Table_A A FULL OUTER JOIN Table_B B ON A.id = B.id;

SQL 中各种连接 JOIN
SQL 中各种连接 JOIN

SELECT * FROM Table_A A FULL OUTER JOIN Table_B B ON A.id = B.id
WHERE A.id IS NULL OR B.id IS NULL;

SQL 中各种连接 JOIN
SQL 中各种连接 JOIN
参考(图片来源)地址