mysql join(左连接,右连接,外连接,内连接)

内连接(inner join)

SELECT * FROM test a INNER JOIN test1 b on a.id=b.testid

左外连接(left join)(保留左表所有,没有对应的用null占位)

SELECT * FROM test a LEFT JOIN test1 b ON a.id=b.testid

左内连接(左外连接中 使用右表null占位的)

SELECT * FROM test a LEFT JOIN test1 b ON a.id=b.testid WHERE b.testid is null

右外连接(right join)(保留右表所有,没有对应的用null占位)

SELECT * FROM test a RIGHT JOIN test1 b ON a.id=b.testid

右内连接(右外连接中 使用左表null占位的)

SELECT * FROM test a RIGHT JOIN test1 b ON a.id=b.testid WHERE a.id is null

外连接(将左连接和右连接 结果 union组合去重)

SELECT * FROM test a LEFT JOIN test1 b ON a.id=b.testid
UNION
SELECT * FROM test a RIGHT JOIN test1 b ON a.id=b.testid

内连接(外连接 中 左表右表中用null占位的)

SELECT * FROM test a LEFT JOIN test1 b ON a.id=b.testid WHERE b.testid is null
UNION
SELECT * FROM test a RIGHT JOIN test1 b ON a.id=b.testid WHERE a.id is null

浙公网安备 33010602011771号