mysql 多表查詢

2021-08-02 15:08:14 字數 4372 閱讀 7050

很多應用場景下,資料查詢會涉及多張表,即多表查詢。這些表之間存在鏈結關係。

多表鏈結分為:內鏈結,外鏈結, 交叉鏈結。

針對內外鏈結對應不同的鏈結方法

其中,

1. join和inner join是等價的

2. left join和left outer join是等價的,right join和right outer join是等價的;

3. union本來不屬於鏈結方法,用來組合其他鏈結方法實現新的鏈結方法。

下面通過例項對這些鏈結方法進行描述,首先建立兩張表

表a:

+------+---------+

| c_id | country |

+------+---------+

| 1 | china |

| 2 | uk |

| 3 | usa |

+------+---------+

表b:

+------+--------+

| n_id | name |

+------+--------+

| 2 | mike |

| 3 | hanson |

| 4 | jack |

+------+--------+

使用inner join或者join

select a.*, b.*

from a join b

on a.c_id = b.n_id;

或者select a.*, b.*

from a inner join b

on a.c_id = b.n_id;

結果:+------+---------+------+--------+

| c_id | country | n_id | name |

+------+---------+------+--------+

| 2 | uk | 2 | mike |

| 3 | usa | 3 | hanson |

+------+---------+------+--------+

求兩個表的交集

使用left outer join或者left join

select a.*, b.*

from a left join b

on a.c_id = b.n_id;

或者:select a.*, b.*

from a left outer join b

on a.c_id = b.n_id;

結果:+------+---------+------+--------+

| c_id | country | n_id | name |

+------+---------+------+--------+

| 2 | uk | 2 | mike |

| 3 | usa | 3 | hanson |

| 1 | china | null | null |

+------+---------+------+--------+

以a表為基準,b表中沒有的直接補null

這樣得到的結果是,a的所有資料和滿足一定條件的b的部分資料

select a.*, b.*

from a left join b

on a.c_id = b.n_id

where b.n_id is null;

+------+---------+------+------+

| c_id | country | n_id | name |

+------+---------+------+------+

| 1 | china | null | null |

+------+---------+------+------+

select a.*, b.* 

from a right join b

on a.c_id = b.n_id

where a.c_id is null;

+------+---------+------+------+

| c_id | country | n_id | name |

+------+---------+------+------+

| null | null | 4 | jack |

+------+---------+------+------+

select a.*, b.* 

from a right join b

on a.c_id = b.n_id;

+------+---------+------+--------+

| c_id | country | n_id | name |

+------+---------+------+--------+

| 2 | uk | 2 | mike |

| 3 | usa | 3 | hanson |

| null | null | 4 | jack |

+------+---------+------+--------+

mysql不支援這種鏈結,但可以使用left join union right join來實現

select a.*, b.*

from a left join b

on a.c_id = b.n_id

union

select a.*, b.*

from a right join b

on a.c_id = b.n_id;

+------+---------+------+--------+

| c_id | country | n_id | name |

+------+---------+------+--------+

| 2 | uk | 2 | mike |

| 3 | usa | 3 | hanson |

| 1 | china | null | null |

| null | null | 4 | jack |

+------+---------+------+--------+

select a.*, b.*

from a left join b

on a.c_id = b.n_id

where b.n_id is null

union

select a.*, b.*

from a right join b

on a.c_id = b.n_id

where a.c_id is null;

+------+---------+------+------+

| c_id | country | n_id | name |

+------+---------+------+------+

| 1 | china | null | null |

| null | null | 4 | jack |

+------+---------+------+------+

得到兩個表的笛卡爾積

select a.* 

from a cross join b;

+------+---------+

| c_id | country |

+------+---------+

| 1 | china |

| 2 | uk |

| 3 | usa |

| 1 | china |

| 2 | uk |

| 3 | usa |

| 1 | china |

| 2 | uk |

| 3 | usa |

+------+---------+

mysql多表 MySQL 多表查詢

多表查詢 select listname from tablename1,tablename2 笛卡爾積 多表查詢中,如果沒有連線條件,則會產生笛卡爾積 數學中的定義 假設集合a 集合b 則兩個集合的笛卡爾積為 實際執行環境下,應避免使用笛卡爾積 解決方案 在where加入有效的連線條件 等值連線 ...

mysql多表查詢方式 MySQL多表查詢方式問題

你的 sql 沒有用到任何索引,對 a b 兩個表都是全表掃瞄,在資料量小的時候是沒有問題的,但是如果資料量超過 100 萬,效能問題就會突顯出來。這裡不清楚你的 created at 欄位是什麼型別,不過從你的 date format created at,y m d 看來,應該是 datetim...

mysql 多表查詢or MySQL 多表查詢

前期準備 建表create table dep id int,name varchar 20 create table emp id int primary key auto increment,name varchar 20 enum male female not null default ma...