python怎样实现多表连接_Python Day45多表连接查询
一、多表連接查詢
1 交叉連接:不適用任何匹配條件。生成笛卡爾積
mysql> select * fromemployee,department;+----+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技術(shù) |
| 1 | egon | male | 18 | 200 | 201 | 人力資源 |
| 1 | egon | male | 18 | 200 | 202 | 銷售 |
| 1 | egon | male | 18 | 200 | 203 | 運(yùn)營(yíng) |
| 2 | alex | female | 48 | 201 | 200 | 技術(shù) |
| 2 | alex | female | 48 | 201 | 201 | 人力資源 |
| 2 | alex | female | 48 | 201 | 202 | 銷售 |
| 2 | alex | female | 48 | 201 | 203 | 運(yùn)營(yíng) |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技術(shù) |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 銷售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 運(yùn)營(yíng) |
| 4 | yuanhao | female | 28 | 202 | 200 | 技術(shù) |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力資源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 銷售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 運(yùn)營(yíng) |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術(shù) |
| 5 | liwenzhou | male | 18 | 200 | 201 | 人力資源 |
| 5 | liwenzhou | male | 18 | 200 | 202 | 銷售 |
| 5 | liwenzhou | male | 18 | 200 | 203 | 運(yùn)營(yíng) |
| 6 | jingliyang | female | 18 | 204 | 200 | 技術(shù) |
| 6 | jingliyang | female | 18 | 204 | 201 | 人力資源 |
| 6 | jingliyang | female | 18 | 204 | 202 | 銷售 |
| 6 | jingliyang | female | 18 | 204 | 203 | 運(yùn)營(yíng) |
+----+------------+--------+------+--------+------+--------------+
2 內(nèi)連接:只連接匹配的行
#找兩張表共有的部分,相當(dāng)于利用條件從笛卡爾積結(jié)果中篩選出了正確的結(jié)果#department沒有204這個(gè)部門,因而employee表中關(guān)于204這條員工信息沒有匹配出來
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;+----+-----------+------+--------+--------------+
| id | name | age | sex | name |
+----+-----------+------+--------+--------------+
| 1 | egon | 18 | male | 技術(shù) |
| 2 | alex | 48 | female | 人力資源 |
| 3 | wupeiqi | 38 | male | 人力資源 |
| 4 | yuanhao | 28 | female | 銷售 |
| 5 | liwenzhou | 18 | male | 技術(shù) |
+----+-----------+------+--------+--------------+
#上述sql等同于
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
3 外鏈接之左連接:優(yōu)先顯示左表全部記錄
#以左表為準(zhǔn),即找出所有員工信息,當(dāng)然包括沒有部門的員工#本質(zhì)就是:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒有的結(jié)果
mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;+----+------------+--------------+
| id | name | depart_name |
+----+------------+--------------+
| 1 | egon | 技術(shù) |
| 5 | liwenzhou | 技術(shù) |
| 2 | alex | 人力資源 |
| 3 | wupeiqi | 人力資源 |
| 4 | yuanhao | 銷售 |
| 6 | jingliyang | NULL |
+----+------------+--------------+
4 外鏈接之右連接:優(yōu)先顯示右表全部記錄
#以右表為準(zhǔn),即找出所有部門信息,包括沒有員工的部門#本質(zhì)就是:在內(nèi)連接的基礎(chǔ)上增加右邊有左邊沒有的結(jié)果
mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;+------+-----------+--------------+
| id | name | depart_name |
+------+-----------+--------------+
| 1 | egon | 技術(shù) |
| 2 | alex | 人力資源 |
| 3 | wupeiqi | 人力資源 |
| 4 | yuanhao | 銷售 |
| 5 | liwenzhou | 技術(shù) |
| NULL | NULL | 運(yùn)營(yíng) |
+------+-----------+--------------+
5 全外連接:顯示左右兩個(gè)表全部記錄
全外連接:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒有的和右邊有左邊沒有的結(jié)果#注意:mysql不支持全外連接 full JOIN#強(qiáng)調(diào):mysql可以使用此種方式間接實(shí)現(xiàn)全外連接
select * from employee left join department on employee.dep_id =department.id
union
select* from employee right join department on employee.dep_id =department.id
;#查看結(jié)果
+------+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技術(shù) |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術(shù) |
| 2 | alex | female | 48 | 201 | 201 | 人力資源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 銷售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 運(yùn)營(yíng) |
+------+------------+--------+------+--------+------+--------------+
#注意 union與union all的區(qū)別:union會(huì)去掉相同的紀(jì)錄
二、子查詢
1:子查詢是將一個(gè)查詢語句嵌套在另一個(gè)查詢語句中。
2:內(nèi)層查詢語句的查詢結(jié)果,可以為外層查詢語句提供查詢條件。
3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關(guān)鍵字
4:還可以包含比較運(yùn)算符:= 、 !=、> 、
1 帶IN關(guān)鍵字的子查詢
查詢employee表,但dep_id必須在department表中出現(xiàn)過
select* fromemployee
where dep_idin(select idfrom department);
2 帶比較運(yùn)算符的子查詢
#比較運(yùn)算符:=、!=、>、>=、#查詢平均年齡在25歲以上的部門名
select id,name fromdepartment
where idin(select dep_idfrom employee group by dep_id having avg(age) > 25);#查看技術(shù)部員工姓名
select name fromemployee
where dep_idin(select idfrom department where name='技術(shù)');#查看不足1人的部門名
select name fromdepartment
where idin(select dep_idfrom employee group by dep_id having count(id) <=1);
3 帶EXISTS關(guān)鍵字的子查詢
EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時(shí),內(nèi)層查詢語句不返回查詢的記錄。
而是返回一個(gè)真假值。True或False
當(dāng)返回True時(shí),外層查詢語句將進(jìn)行查詢;當(dāng)返回值為False時(shí),外層查詢語句不進(jìn)行查詢
#department表中存在dept_id=203,Ture
mysql> select * fromemployee->where exists-> (select id from department where id=200);+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+
#department表中存在dept_id=205,False
mysql> select * fromemployee->where exists-> (select id from department where id=204);
Empty set (0.00 sec)
總結(jié)
以上是生活随笔為你收集整理的python怎样实现多表连接_Python Day45多表连接查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语言条件语序心得_教师心得:提高34岁幼
- 下一篇: websocket python爬虫_p