mysql 多表聯查

2021-07-09 10:16:39 字數 4507 閱讀 7862

1. 多表連線型別

2. 1. 笛卡爾積( 交叉連線) 在 mysql 中可以為 cross join 或者省略 cross 即 join,或 

者使用』,』 如: 

1. select * from table1 cross join table2 

2. select * from table1 join table2 

3. select * from table1,table2 

由於其返回的結果為被連線的兩個資料表的乘積,因此當有 where, on 或 using 

條件的時候一般不建議使用,因為當資料表專案太多的時候,會非常慢。用 一般使用 left 

[outer] join 或者 right [outer] join 

2. 內連線 inner join 在 mysql 中把 inner join 叫做等值連線,即需要指定等值連 

接條件在 mysql 中 cross 和 inner join 被劃分在一起。 join_table: table_reference 

[inner | cross] join table_factor [join_condition] 

3. mysql 中的外連線,分為左外連線和右連線,即除了返回符合連線條件的結果之外,還 

要返回左表(左連線)或者右表(右連線)中不符合連線條件的結果, 相對應的使用 null 對應。 

例子: 

user 表: 

id | name 

——— 

1 | libk 

2 | zyfon 

3 | daodao 

user_action 表: 

user_id | action 

————— 

1 | jump 

1 | kick 

1 | jump 

2 | run 

4 | swim 

sql: 

1. select id, name, action from user as u 

2. left join user_action a on u.id = a.user_id 

result: 

id | name | action 

——————————– 

1 | libk | jump ① 

1 | libk | kick ② 

1 | libk | jump ③ 

2 | zyfon | run ④ 

3 | daodao | null ⑤ 

分析: 

注意到 user_action 中還有乙個 user_id=4, action=swim 的紀錄,但是沒有在結果中出現, 

而 user 表中的 id=3, name=daodao 的使用者在 user_action 中沒有相應的紀錄,但是卻出現 

在了結果集中 

因為現在是 left join,所有的 工作以 left 為準. 

結果 1,2,3,4 都是既在左表又在右表的紀錄,5 是只在左表,不在右表的紀錄 

工作原理: 

從左表讀出一條,選出所有與 on 匹配的右表紀錄(n 條)進行連線,形成 n 條紀錄(包括重複 

的行,如:結果 1 和結果 3),如果右邊沒有與 on 條件匹配的表,那連線的字段都是 null. 

然後繼續讀下一條。 

引申: 

我們可以用右表沒有 on 匹配則顯示 null 的規律, 來找出所有在左表, 不在右表的紀錄, 注 

意用來判斷的那列必須宣告為 not null 的。 

如: sql: 

1. select id, name, action from user as u 

2. left join user_action a on u.id = a.user_id 

3. where a.user_id is null 

( 注意: 

1.列值為 null 應該用 is null 而不能用=null 

2.這裡 a.user_id 列必須宣告為 not null 的. 

) 上面 sql 的 result: 

id | name | action 

————————– 

3 | daodao | null 

——————————————————————————– 

一般用法: 

a. left [outer] join: 

除了返回符合連線條件的結果之外, 還需要顯示左表中不符合連線條件的資料列, 相對應使 

用 null 對應 

1. select column_name from table1 left [ outer ] join table2 on table1.column=ta 

ble2.column 

b. right [outer] join: 

right 與 left join 相似不同的僅僅是除了顯示符合連線條件的結果之外, 還需要顯示右 

表中不符合連線條件的資料列,相應使用 null 對應 

1. select column_name from table1 right [ outer ] join table2 on table1.column=t 

able2.column 

tips: 

1. on a.c1 = b.c1 等同於 using(c1) 

2. inner join 和 , ( 逗號) 在語義上是等同的 

3. 當 mysql 在從乙個表中檢索資訊時,你可以提示它選擇了哪乙個索引。 

如果 explain 顯示 mysql 使用了可能的索引列表中錯誤的索引,這個特性將是很有用的。 

通過指定 use index (key_list) ,你可以告訴 mysql 使用可能的索引中最合適的乙個索引在表中查詢 

記錄行。 

可選的二選一句法 ignore index (key_list) 可被用於告訴 mysql 不使用特定的索引。如: 

1. mysql> select * from table1 use index (key1,key2) 

2. -> where key1=1 and key2=2 and key3=3; 

3. mysql> select * from table1 ignore index (key3) 

4. -> where key1=1 and key2=2 and key3=3;

2. 表連線的約束條件

新增顯示條件 where, on, using

where 子句 

mysql>

select * from table1,table2 where table1.id=table2.id;

on mysql>

select * from table1 left join table2 on table1.id=table2.id;

select * from table1 left join table2 on table1.id=table2.id

left join table3 on table2.id=table3.id;

using 子句,using()用於兩張表的join查詢,要求using()指定的列在兩個表中均存在,並使用之用於join的條件。 

示例: 

select a., b. from a left join b using(cola); 

等同於: 

select a., b. from a left join b on a.cola = b.cola;

連線多於兩個表的情況舉例: 

mysql> 

1. select artists.artist, cds.title, genres.genre 

2. 3. from cds 

4. 5. left join genres n cds.genreid = genres.genreid 

6. 7. left join artists on cds.artistid = artists.artistid; 

或者 mysql> 

1. select artists.artist, cds.title, genres.genre 

2. 3. from cds 

4. 5. left join genres on cds.genreid = genres.genreid 

6. 7. left join artists -> on cds.artistid = artists.artistid

where (genres.genre = 『pop』 ); 

另外需要注意的地方 在 mysql 中涉及到多表查詢的時候,需要根據查詢的情況,想好使 

用哪種連線方式效率更高。

交叉連線(笛卡爾積)或者內連線 [inner | cross] join

左外連線 left [outer] join 或者右外連線 right [outer] join 注意指定連線條 

件 where, on,using.

3. mysql 如何優化 left join 和 和 right join

mysql 多表聯查 MySQL的多表聯查

今天是周二,我們一起來研究下mysql的多表聯查啊。或許你也知道,表之間的關係有 1對1 1對多 多對多。然後.1.巢狀查詢 乙個查詢的結果是另外sql查詢的條件 如 查詢stu表中年齡最大的是誰?mysql select from stu where age select max age from...

mysql多表聯查 mysql 多表聯查 例項

多表查詢 笛卡爾積查詢 笛卡爾積查詢 就是兩張表相乘,若左邊表有m條資訊,右邊表有n條資訊,那麼查詢顯示的資訊總共為m n條,這其中往往包含大量錯誤資料,需要用where 條件來過濾無用資訊 笛卡爾積查詢語句 select from dept,emp id name id name dept id ...

mysql多表聯查

1.交叉查詢 笛卡爾積 基本不用 2.內連線查詢 3.外鏈結 1 左外連線 2 右外連線 4.子查詢 巢狀查詢 eg 查詢分類名稱是手機數碼的商品 select from product p where p.cno in select cid from category where cname 手機...