python學習筆記 day43 多表聯合查詢+連接查詢


首先創建兩張表,person表 和dept表(兩者關聯的字段是dept_id 就是部門號)

第一張表 person 人員信息表

create table person(
  id int not null auto_increment primary key,
  name varchar(50) not null,
  age int not null,
  sex char(2) not null,
  salary int not null,
  hire_date datetime default null,    # insert 數據時 可以手動輸入員工入職時間
  create_time timestamp not null default current_timestamp,  # 可以記錄創建數據的時間(insert數據時可以不用自己添加,會自動生成)   
  update_time timestamp not null default current_timestamp on update current_timestamp,  # 每次修改數據項,會自動記錄 update時間
  dept_id int null)

insert into person(id,name,age,sex,salary,hire_date,dept_id) values(1,"璇璇",22,"女",20000,"2017-09-01",1);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(2,"西西",22,"女",16000,"2017-09-01",1);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(3,"楠楠",24,"男",18000,"2017-09-01",1);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(4,"東東",24,"男",22000,"2017-09-01",2);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(5,"哈哈",23,"男",21000,"2017-09-01",2);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(6,"呵呵",24,"女",16000,"2017-09-01",3);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(7,"梅梅",26,"女",15000,"2016-09-01",3);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(8,"浩浩",24,"男",18000,"2018-09-01",4);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(9,"夏夏",27,"女",19000,"2015-09-01",4);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(10,"星星",29,"女",12000,"2016-09-01",5);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(11,"alex",26,"男",30000,"2011-09-01",5);
insert into person(id,name,age,sex,salary,hire_date,dept_id) values(12,"Eva-J",29,"女",29000,"2012-09-01",6);
select * from person;

 

運行結果:

 

 第二張表: dept----記錄部門id以及對應的部門名稱

create table dept(
  did int not null,
  name varchar(50) not null,
  create_time timestamp not null default current_timestamp,
  update_time timestamp not null default current_timestamp on update current_timestamp 
  )
insert into dept(did,name) values(1,"研發部");
insert into dept(did,name) values(2,"事業部");
insert into dept(did,name) values(3,"銷售部");
insert into dept(did,name) values(4,"運營部");
insert into dept(did,name) values(5,"美工部");
insert into dept(did,name) values(6,"產品部");
select * from dept;

運行結果:


哦對了,為了后續講多表聯合查詢,連接查詢的區別,這里我再往person表 和 dept表各自增加一條信息:

insert into person(id,name,age,sex,salary,hire_date) values(13,"萌萌",25,"女",18000,"2015-02-01");  # 新增加的員工沒有部門
insert into dept(did,name) values(7,"國際部")  # dept表有個部門7 person表沒有員工在這個部門

運行結果:


 

1. 多表聯合查詢-----兩張表的交集

如果是想查詢person表的全部人員信息,而且需要顯示各自的部門:需要用到聯合查詢

 

select * from person,dept where person.dept_id=dept.did;   # 可以查找person表中所有員工信息,並且顯示每一位員工的部門名稱(不單單是部門id)

運行結果:

 

 

 

其實會發現只顯示了12條數據(也就是person表中第十三條數據(沒有dept_id的)並沒有被顯示)因為where條件是按照兩張表的dept_id 和did對應相等來聯合查詢的;

注意:

多表聯合查詢時一定要找到兩個表中相互關聯的字段,並且作為條件使用(不加條件顯示笛卡爾乘積的條數--有重疊)

 

2.連接查詢---左連接,右連接,內連接,全連接

2.1 左連接

如果是想顯示person表的全部員工信息,有dept_id的就顯示部門名稱,沒有的也顯示該條員工信息,只不過部門名稱null即可 這時候就需要用到左連接 :

 

select * from person left join dept on person.dept_id = dept.did;  # 左連接,會顯示person表的全部信息,有對應的dept_id就顯示名稱,沒有也顯示該條信息,只不過部門名稱null即可

運行結果:

 

2.2 右連接

 如果是想顯示全部部門信息,即使有的部門沒有對應的員工在,也進行顯示,就需要用到右連接(其實左連接也可以實現,就是把兩個表順序換了)

select * from person right join dept on person.dept_id= dept.did;

運行結果:

 

 2.3 內鏈接----跟聯合查詢的效果是一樣的,只會顯示兩張表重疊的信息(交集)

select * from person inner join dept on person.dept_id =dept.did;  #內連接,顯示兩張表的重疊部門信息

運行結果:

 

 2.4 全連接 UNION

 

select * from person left join dept on person.dept_id =dept.did 
UNION 
select * from person right join dept on person.dept_id = dept.did;  # 全連接,使用UNION關鍵字,顯示兩張表的全部信息;

 

 運行結果:

多表聯合查詢與左右連接的區別:

前者只會找兩表關聯字段中一一對應的重疊(交集)部門數據,而左右連接查詢是不管匹配與否,,只會顯示左邊表全部信息,能匹配就匹配,匹配不了就不顯示(null)

 多表聯合查詢把符合條件的拿出來,不符合的扔掉,而左右連接查詢,一張表作為基准,另一張表去匹配,匹配上的拿過來,匹配不上的用null;

全連接查詢是在內連接基礎上加左右兩邊沒顯示的數據;

注意mysql並不支持full join 關鍵字,但是mysql提供了union關鍵字,使用union可以間接實現full join的功能;

 

 

3. 作業

3.1 查詢出產品部年齡大於22歲工資小於29000的員工,並且按照薪資倒序排列;

select * from person where age>20 and salary<30000 and dept_id = (select did from dept where name="產品部") order by salary desc;

運行結果:

3.2 查詢每個部門中最高工資與最低工資是多少,並且顯示部門名稱;

其實一看這個題就知道需要用到分組查詢每個部門的,group by dept_id。。。)連接查詢(因為顯示person表可以看到工資,但是只有部門ID 沒有部門名稱,而dept表只有部門名稱 沒有人員,工資等)又需要顯示前一張表的所有人員信息,所以是左連接查詢(person在前)

 

select max(salary),min(salary),dept.name from person left join dept on person.dept_id =dept.did group by dept_id ;  # 連接查詢一定要加兩張表的關聯條件(關鍵字on)!!group by需要放在后面 limit 倒數第一,froup by倒數第二,另外當兩個表字段一樣時,需要加上表名.字段名 這樣來區分~

 

運行結果:(person表中分組后每一個小組信息都要顯示,盡管第一行 數據沒有dept_id 也要顯示出來,左連接~)


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM