mysql的左联结与右连接

左连接where只影向右表,右连接where只影响左表。

左连接后的检索结果是显示tbl1的所有数据和tbl2中满足where 条件的数据。

左连接

SELECT

test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
;

右连接


SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;

全连接

SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
union

SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;


选出test1中有test2中没有的

SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
WHERE test2.id is NULL
;


你可能感兴趣的:(专业)