日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python怎样实现多表连接_Python Day45多表连接查询

發(fā)布時(shí)間:2025/3/15 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎样实现多表连接_Python Day45多表连接查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、多表連接查詢

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。