您的位置:首页 > 数据库 > MySQL

mysql的左联结与右连接

2015-09-05 22:37 519 查看
左连接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

;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: