mysql 学习笔记 多表查询02
生活随笔
收集整理的這篇文章主要介紹了
mysql 学习笔记 多表查询02
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
把一張表 想象成兩張表,進行多表查詢
舉例:
以上是 利用表的多表查詢
還可以使用子查詢 來實現
比如:
復雜一些的表查詢舉例:
A:
有兩張表,分別是員工信息表,與部門信息表,要求查詢,各個部門工資最高的員工的信息
員工表:
部門表:
mysql> select * from department; +----+--------+ | id | name | +----+--------+ | 1 | it | | 2 | kuaiji | | 3 | yunwei | +----+--------+思考步驟:
1、從employee表里查詢出每個部門的最高薪資,作為一張臨時表 t
2、將臨時表 t 和 employee表進行內連接,并新增一列,顯示employee表里每個員工所在部門對應的最高薪資
mysql> select e.id,e.name,e.salary, t.maxsalary,t.depid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid; +----+-------+--------+-----------+-------+ | id | name | salary | maxsalary | depid | +----+-------+--------+-----------+-------+ | 1 | Joe | 70000 | 80000 | 1 | | 2 | Tom | 80000 | 80000 | 1 | | 3 | Mary | 50000 | 50000 | 2 | | 4 | Tk | 10000 | 780000 | 3 | | 5 | Inter | 20000 | 780000 | 3 | | 6 | Janet | 780000 | 780000 | 3 | | 7 | Li | 75000 | 80000 | 1 | | 8 | Wang | 2000 | 780000 | 3 | | 9 | Gao | 5000 | 50000 | 2 | | 10 | ZhaoF | 1000 | 50000 | 2 | | 11 | ZhaoX | 2000 | 50000 | 2 | | 12 | Mx | 5000 | 80000 | 1 | | 13 | Mi | 6000 | 80000 | 1 | +----+-------+--------+-----------+-------+3、再用 employee表里每個員工的薪資字段salary 和 部門最高薪資字段列maxsalary進行判斷,查詢出相等數據,此處則查詢出了每個部門最高薪資的員工有哪些,作為表 tt
mysql> select e.id,e.name,e.salary, t.maxsalary,t.depid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid where e.salary=t.maxsalary; +----+-------+--------+-----------+-------+ | id | name | salary | maxsalary | depid | +----+-------+--------+-----------+-------+ | 2 | Tom | 80000 | 80000 | 1 | | 3 | Mary | 50000 | 50000 | 2 | | 6 | Janet | 780000 | 780000 | 3 | +----+-------+--------+-----------+-------+4、因為表 tt 沒有部門名稱,所以我們再將表 tt 和department 表進行內鏈接,查詢部門id相等的數據,從而查詢出每個員工所在的部門名稱
mysql> select tt.*, d.name as departname from (select e.id,e.name,e.salary, t.maxsalary,t.depid as tepid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid where e.salary=t.maxsalary) as tt inner join department as d on tt.tepid=d.id order by tt.tepid, tt.id; +----+-------+--------+-----------+-------+------------+ | id | name | salary | maxsalary | tepid | departname | +----+-------+--------+-----------+-------+------------+ | 2 | Tom | 80000 | 80000 | 1 | it | | 3 | Mary | 50000 | 50000 | 2 | kuaiji | | 6 | Janet | 780000 | 780000 | 3 | yunwei | +----+-------+--------+-----------+-------+------------+B:
查詢 那些 比本部門 平均工資 高的員工的信息
另外一種寫法:
mysql> select tt.*, d.dname from (select s.stid, s.stname,s.stsal,temp.avgsal, s.stdepno from (select stdepno, avg(stsal) as avgsal from staff group by stdepno) as temp , staff as s where temp.stdepno=s.stdepno and s.stsal > temp.avgsal) as tt , department as d where tt.stdepno = d.deptno;還有另一種簡潔的寫法:
select s1.* from staff as s1 where s1.stsal > (select avg(stsal) from staff as s2 where s2.stdepno = s1.stdepno );總結
以上是生活随笔為你收集整理的mysql 学习笔记 多表查询02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多囊能做试管婴儿吗
- 下一篇: mysql 学习笔记15 子查询