mysql學習筆記14 多表查詢初步

  1. 對數據分組的總結

在這裏插入圖片描述
舉例:統計各個部門的平均工資,並且是大於1000的,並且按照平均工資從高到底排序

mysql> select avg(stsal) as myavgsal, stdepno from staff group by stdepno having myavgsal > 1000 order by myavgsal desc;
+-------------+---------+
| myavgsal    | stdepno |
+-------------+---------+
| 7000.200000 |      10 |
| 5350.200000 |      30 |
| 3094.766667 |      20 |
+-------------+---------+
  1. 笛卡爾積的原理
    在這裏插入圖片描述
    多表查詢練習:
  2. 顯示員工名,工資,及所在部門的名字
select staff.stname , staff.stsal, department.dname from staff, department where department.deptno = staff.stdepno;
  1. 顯示部門號爲10的部門名,員工名,工資,
select staff.stname , staff.stsal, department.dname from staff, department where department.deptno = staff.stdepno and department.deptno = 10;
  1. 顯示每個員工的姓名,工資, 及其工資的級別
select staff.stname, staff.stsal, salalevel.slevel from staff, salalevel where staff.stsal >= slower and staff.stsal <= shig;
  1. 顯示每個員工姓名,工資,以及部門名,以及工資級別
    這樣就設計到三張表的關聯查詢,三張表爲了避免笛卡爾積, 就需要至少兩個查詢條件
select staff.stname, staff.stsal , department.dname, salalevel.slevel from staff,department,salalevel where staff.stdepno = department.deptno and staff.stsal >= salalevel.slower and staff.stsal <= salalevel.shig;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章