Mysql 多表查詢
Select * from tablea inner join tableb on tablea.id = tableb.id where tableb.id is null;
Select * from tablea eft join tableb on tablea.id = tableb.id where tableb.id is null;
1蹋凝、多表查詢介紹
在實(shí)際應(yīng)用中
MySQL大部分情況下鲁纠,查詢語句都會涉及到多張表格:
其中多表查詢的分類有:內(nèi)連接,外連接和交叉連接
A內(nèi)連接:
join, inner join
B外鏈接:
left join, left outer join, right join, right outer join, union
C交叉連接:
cross join
2鳍寂、實(shí)例
假設(shè)有兩張表:
TableA:
TableB:
2.1 內(nèi)連接(只有一種場景)
Inner join 或者 join(等同于 inner join)
Select a., b. from tablea a
inner join tableb b
On a.id = b.id;
或者:
Select a.,b. from tablea a
Join tableb b
On a.id = b.id;
應(yīng)用場景:
注意:
有一種連接為
自然連接:
nature join, 假如執(zhí)行:
Select * form Tablea a nature join tableb b;
這種連接和內(nèi)連接相似改含,但是輸出的結(jié)果中,會將相同的列去掉迄汛,即上述中
id列只會有一列捍壤,不會有相同的兩列骤视。
2.2 外連接(六種場景)
2.2.1 left join 或者left outer join(等同于left join)
select a., b. from tablea a
left join tableb b
on a.id = b.id
或者
select a., b. from tablea a
left outer join tableb b
on a.id = b.id
結(jié)果如下,
TableB中更不存在的記錄填充Null:
應(yīng)用場景
:
這種場景下得到的是
A的所有數(shù)據(jù)鹃觉,和滿足某一條件的B的數(shù)據(jù);
2.2.2 [left join 或者left outer join(等同于left join)] + [where B.column is null]
select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
Where b.id is null
結(jié)果如下
:
應(yīng)用場景
:
這種場景下得到的是
A中的所有數(shù)據(jù)減去"與B滿足同一條件 的數(shù)據(jù)"专酗,然后得到的A剩余數(shù)據(jù);
2.2.3 right join 或者fight outer join(等同于right join)
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
結(jié)果如下盗扇,
TableA中更不存在的記錄填充Null:
應(yīng)用場景
:
這種場景下得到的是
B的所有數(shù)據(jù)祷肯,和滿足某一條件的A的數(shù)據(jù);
2.2.4 [left join 或者left outer join(等同于left join)] + [where A.column is null]
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
where a.id is null
結(jié)果如下
:
[
應(yīng)用場景
:
這種場景下得到的是
B中的所有數(shù)據(jù)減去 "與A滿足同一條件 的數(shù)據(jù)“疗隶,然后得到的B剩余數(shù)據(jù)佑笋;
2.2.5 full join (mysql不支持,但是可以用 left join union right join代替)
select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
union
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
union過后抽减,重復(fù)的記錄會合并(id為2允青,3,4的三條記錄)卵沉,所以結(jié)果如下:
應(yīng)用場景:
這種場景下得到的是滿足某一條件的公共記錄颠锉,和獨(dú)有的記錄
2.2.6 full join + is null(mysql不支持,但是可以用 (left join + is null) union (right join+isnull代替)****
select a.id aid,a.age,b.id bid,b.name from tablea a
left join tableb b
on a.id = b.id
where b.id is null
union
select a.id aid,a.age,b.id bid,b.name from tablea a
right join tableb b
on a.id = b.id
where a.id is null
再添加一個
where語句進(jìn)行擴(kuò)充
結(jié)果如下
:
應(yīng)用場景
:
這種場景下得到的是
A史汗,B中不滿足某一條件的記錄之和
注
:上面共有其中七(2^3-1)種應(yīng)用場景琼掠,還有一種是全空白,那就是什么都不查停撞,七種情形包含了實(shí)際應(yīng)用所有可能的場景
2.3 交叉連接 (cross join)
2.3.1 實(shí)際應(yīng)用中還有這樣一種情形瓷蛙,想得到A,B記錄的排列組合戈毒,即笛卡兒積艰猬,這個就不好用集合和元素來表示了。需要用到cross join:
select a.id aid,a.age,b.id bid,b.name from tablea a
cross join tableb b
**2.3.2 還可以為cross join指定條件 (where):****
**
select a.id aid,a.age,b.id bid,b.name from tablea a
cross join tableb b
where a.id = b.id
結(jié)果如下
注
:這種情況下實(shí)際上實(shí)現(xiàn)了內(nèi)連接的效果