CREATE DATABASE test;
use test;
CREATE TABLE student (
id int,
name varchar(255),
address varchar(255),
city varchar(255),
uid varchar(255)
);
CREATE TABLE teacher (
id int,
name varchar(255),
address varchar(255),
uid varchar(255)
);
INSERT INTO student VALUES (9, '吴九', '孙七家隔壁', '杭州','12345');
INSERT INTO student VALUES (10, '郑十', '周八家隔壁', '杭州','45678');
INSERT INTO student VALUES (8, '周八', '雷峰塔附近', '杭州','56789');
INSERT INTO teacher VALUES (1, '李老师', '杭州','12345');
INSERT INTO teacher VALUES (2, '吴老师', '杭州','12345');
INSERT INTO teacher VALUES (1, '张老师', '杭州','45678');
INSERT INTO teacher VALUES (1, '王老师', '杭州','78910');
SELECT A.name,A.address,A.uid,B.name FROM student A INNER JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A LEFT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A RIGHT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
周八 雷峰塔附近 56789 NULL
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
NULL NULL NULL 王老师
INSERT INTO student VALUES (9, '吴九', '孙七家隔壁', '杭州','12345');
INSERT INTO student VALUES (10, '郑十', '周八家隔壁', '杭州','45678');
INSERT INTO student VALUES (8, '周八', '雷峰塔附近', '杭州','56789');
INSERT INTO student VALUES (11, '吴九', '孙七家隔壁', '杭州','12345');
INSERT INTO teacher VALUES (1, '李老师', '杭州','12345');
INSERT INTO teacher VALUES (1, '张老师', '杭州','45678');
INSERT INTO teacher VALUES (1, '王老师', '杭州','78910');
SELECT A.name,A.address,A.uid,B.name FROM student A INNER JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A LEFT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A RIGHT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
name address uid name
吴九 孙七家隔壁 12345 李老师
郑十 周八家隔壁 45678 张老师
吴九 孙七家隔壁 12345 李老师
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 李老师
郑十 周八家隔壁 45678 张老师
周八 雷峰塔附近 56789 NULL
name address uid name
吴九 孙七家隔壁 12345 李老师
郑十 周八家隔壁 45678 张老师
吴九 孙七家隔壁 12345 李老师
NULL NULL NULL 王老师
INSERT INTO student VALUES (9, '吴九', '孙七家隔壁', '杭州','12345');
INSERT INTO student VALUES (10, '郑十', '周八家隔壁', '杭州','45678');
INSERT INTO student VALUES (8, '周八', '雷峰塔附近', '杭州','56789');
INSERT INTO student VALUES (11, '吴九', '孙七家隔壁', '杭州','12345');
INSERT INTO teacher VALUES (1, '李老师', '杭州','12345');
INSERT INTO teacher VALUES (2, '吴老师', '杭州','12345');
INSERT INTO teacher VALUES (1, '张老师', '杭州','45678');
INSERT INTO teacher VALUES (1, '王老师', '杭州','78910');
SELECT A.name,A.address,A.uid,B.name FROM student A INNER JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A LEFT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
SELECT A.name,A.address,A.uid,B.name FROM student A RIGHT JOIN (SELECT * FROM teacher) B on A.uid=B.uid;
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
周八 雷峰塔附近 56789 NULL
name address uid name
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
郑十 周八家隔壁 45678 张老师
吴九 孙七家隔壁 12345 李老师
吴九 孙七家隔壁 12345 吴老师
NULL NULL NULL 王老师
一对多的情况,INNER JOIN 可能会出现结果数量比左表大的情况,LEFT JOIN 一定会出现结果数量比左表大,RIGHT JOIN 结果的数量和右表相同
多对一的情况,INNER JOIN 结果数量小于等于左表,LEFT JOIN 结果与左表一致,RIGHT JOIN 结果一定会比右表数量大。
多对多情况,INNER JOIN,LEFT JOIN,RIGHT JOIN 结果数量不可控