表a
a1 a2
1 t1
2 t2
表b
b1 b2
1 u1
3 u3
(1)a INNER JOIN b记录数是a,b交集的记录数
SELECT * FROM a INNER JOIN b ON a.a1=b.b1
a1 a2 b1 b2
1 t1 1 u1
(2)a LEFT JOIN b记录数是a的记录数
LEFT JOIN= LEFT OUTER JOIN
SELECT * FROM a LEFT JOIN b ON a.a1=b.b1
a1 a2 b1 b2
1 t1 1 u1
2 t2
(3)a RIGHT JOIN b记录数是b的记录数
RIGHT JOIN= RIGHT OUTER JOIN
SELECT * FROM a RIGHT JOIN b ON a.a1=b.b1
a1 a2 b1 b2
1 t1 1 u1
3 u3
(4)a FULL JOIN b记录数是a,b并集的记录数
FULL JOIN= FULL OUTER JOIN
SELECT * FROM a FULL JOIN b ON a.a1=b.b1
a1 a2 b1 b2
1 t1 1 u1
2 t2
3 u3
注:
(1)mysql是不支持FULL JOIN的,可以使用UNOIN ALL来实现
(2)当有多个满足条件的记录时,
left join / right join 查询到的总记录数,会比表a / 表b 的总记录数多。
例如,表a
a1 a2
1 t1
2 t2
表b
b1 b2
1 u1 <- 多条满足条件的记录
1 v1 <- 多条满足条件的记录
3 u3
SELECT * FROM a LEFT JOIN b ON a.a1=b.b1
a1 a2 b1 b2
1 t1 1 u1 <- 多条满足条件的记录
1 t1 1 v1 <- 多条满足条件的记录
2 t2