mysql 學習筆記 多表查詢02

把一張表 想象成兩張表,進行多表查詢

舉例:

  1. 查詢 所有員工的 姓名 以及 其 上級姓名
select s1.stname, s2.stname from staff as s1, staff as s2 where s1.stmgr = s2.stid;
  1. 查詢 員工李巖的 上級姓名
select s1.stname, s2.stname from staff as s1, staff as s2 where s1.stmgr = s2.stid and s1.stname='李巖';

以上是 利用表的多表查詢
還可以使用子查詢 來實現
比如:

 select staff.stname from staff where stid = (select stmgr from staff where stname='李巖');

復雜一些的表查詢舉例:
A:
有兩張表,分別是員工信息表,與部門信息表,要求查詢,各個部門工資最高的員工的信息
員工表:

mysql> select * from employee;
+----+-------+--------+-------+
| id | name  | salary | depid |
+----+-------+--------+-------+
|  1 | Joe   |  70000 |     1 |
|  2 | Tom   |  80000 |     1 |
|  3 | Mary  |  50000 |     2 |
|  4 | Tk    |  10000 |     3 |
|  5 | Inter |  20000 |     3 |
|  6 | Janet | 780000 |     3 |
|  7 | Li    |  75000 |     1 |
|  8 | Wang  |   2000 |     3 |
|  9 | Gao   |   5000 |     2 |
| 10 | ZhaoF |   1000 |     2 |
| 11 | ZhaoX |   2000 |     2 |
| 12 | Mx    |   5000 |     1 |
| 13 | Mi    |   6000 |     1 |
+----+-------+--------+-------+

部門表:

mysql> select * from department;
+----+--------+
| id | name   |
+----+--------+
|  1 | it     |
|  2 | kuaiji |
|  3 | yunwei |
+----+--------+

思考步驟:
1、從employee表里查詢出每個部門的最高薪資,作為一張臨時表 t

mysql> select depid , max(salary) as maxsalary from employee group by depid;
+-------+-----------+
| depid | maxsalary |
+-------+-----------+
|     1 |     80000 |
|     2 |     50000 |
|     3 |    780000 |
+-------+-----------+

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 ss.*, d.dname from (select s.stname, s.stid, s.stsal, s.stdepno, t.avgsal from (select stdepno, avg(stsal) as avgsal from staff group by stdepno) as t inner join staff as s on s.stdepno=t.stdepno where s.stsal > t.avgsal ) as ss inner join department as d on ss.stdepno = d.deptno;

另外一種寫法:

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 );

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382462.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382462.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382462.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Mac Redis安裝入門教程

redis安裝(mac) brew install redis 如果需要后臺運行 redis 服務,使用命令 brew services start redis 如果不需要后臺服務,則使用命令 redis-server /usr/local/etc/redis.conf 啟動redis服務 執行以下命令 /usr/local/bin…

Shell 腳本基礎學習

查詢手冊 菜鳥教程 for循環和seq的使用 echo "method 1" for i in seq 1 10; doecho $i; doneecho "method 2" for i in {1..10} doecho $i; doneecho "method 3" for i in seq 1 2 10; doecho $i; done進入目錄創建文件重定向內容 cd Test …

mysql 學習筆記15 子查詢

子查詢定義&#xff1a; 單上子查詢舉例&#xff1a; 顯示與 員工 關平 同一部門的員工&#xff0c; 但不包括關平 select * from staff where staff.stdepno (select staff.stdepno from staff where stname關平) and staff.stname<> 關平 ;多行子查詢舉例&#xff…

shell自學筆記

文章目錄重定向數值比較邏輯操作符使用范圍關于文件判斷測試表達式test [] [[]] (())的區別sed教程AWK教程重定向 0表示標準輸入 1表示標準輸出 2表示標準錯誤輸出 默認為標準輸出重定向&#xff0c;與 1> 相同 2>&1 意思是把 標準錯誤輸出 重定向到 標準輸出. &…

ffmpeg簡單使用小記

1. 使用ffmpeg 進行普通切片&#xff08;ts&#xff09;操作 .\ffmpeg.exe -i a.mp4 -y -f hls -c copy -hls_time 10 .\s.m3u82. 使用ffmpeg 對視頻進行設置旋轉參數為0 .\ffmpeg.exe -i a.mp4 -metadata:s:v:0 rotate0 -c copy outputfile.mp43. 使用文件對視頻進行加密 .\…

python3安裝教程配置配置阿里云

配置全新阿里云 Linux iz2ze0ajic0vbvwnjhw2bwz 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 安裝依賴包 wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc1.tar.xz 新建一個文件夾存放python3 mkdir /u…

python 使用requests模塊進行 視頻文件的下載

公司項目需要下載一批視頻文件&#xff0c; 格式是mp4和mkv的&#xff0c;就借助request模塊進行了下載&#xff0c;前提是源服務器返回文件的大小&#xff0c;以及可以接受 請求頭headers中帶有Range參數 以下是下載邏輯&#xff1a; resp requests.head(urlreal_video_url)…

Git的多人協作和分支處理測試

首先配置ssh密鑰 克隆項目 配置兩臺主機&#xff08;一臺本地mac&#xff0c;一臺云服務器&#xff09;通過這樣的方式模擬多人開發。 創建分支 [root ~/Git_test_多人協作和沖突合并/branch_test]$ ls README.md [root ~/Git_test_多人協作和沖突合并/branch_test]$ git b…

python 碎片整理 threading模塊小計

threading模塊中&#xff0c; start()與run()方法的區別 threading.start() 方法是開啟一個線程 threading.run() 方法則是普通的函數調用

git教程目錄

git入門教程 PyCharm和git安裝教程 Git的多人協作和分支處理測試

msyql 禁止遠程訪問

1. use mysql 2. select host , user from user; 查看用戶 與 對應的host 3. 將 host 中是 %的改為 localhost&#xff0c; 酌情也可以其他用戶 的host限制為localhost update user set host "localhost" where user "root" and host "%" 4. …

mysql索引回表

先索引掃描&#xff0c;再通過ID去取索引中未能提供的數據&#xff0c;即為回表。 建表 mysql> create table T( id int primary key, k int not null, name varchar(16), index (k))engineInnoDB;如果語句是 select * from T where ID500&#xff0c;即主鍵查詢方式&am…

C++ 執行cmd命令 并獲取輸出

這是參考別人的 &#xff0c;具體來源忘了&#xff0c;唉&#xff0c;等想起來一定補上出處 頭文件 PipeCmd.h #ifndef _PIPE_CMD_H_ #define _PIPE_CMD_H_#include <Windows.h>// 執行 cmd 命令, 并獲取執行結果數據 BOOL PipeCmd(char *pszCmd, char *pszResultBuffe…

iterm2 保存阿里云登陸并防止斷開連接

commando edit profiles新增一個頁面 添加命令 ssh -A -p 22 -o ServerAliveInterval60 rootIP

QString中包含中文的時候, 轉為char *

轉載自 https://blog.csdn.net/mihang2/article/details/39026865 QString中包含中文的時候&#xff0c; 轉為char * void FileEncWidget::QString2ANSI(QString text, char **pOut) {std::wstring wIn text.toStdWString();char *pcstr (char *)malloc(sizeof(char)*(2 * w…

brew安裝

官網&#xff1a;http://brew.sh/ 安裝軟件&#xff1a;brew install 軟件名&#xff0c;例&#xff1a;brew install wget搜索軟件&#xff1a;brew search 軟件名&#xff0c;例&#xff1a;brew search wget卸載軟件&#xff1a;brew uninstall 軟件名&#xff0c;例&#…

關于異步IO模型的學習

看到兩篇不錯的文章&#xff0c;轉載了&#xff1a; https://www.cnblogs.com/fanzhidongyzby/p/4098546.html https://www.cnblogs.com/aspirant/p/9166944.html

centos 無法連接網絡

最小化安裝&#xff0c;沒有ifconfig默認沒法聯網 cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-en33 也有可能是其他后綴 找到ONBOOTno service network restart 然后yum install net-tools

C++實現utf8和gbk編碼字符串互相轉換

不同系統或者服務器之間消息傳遞經常遇到編碼轉換問題&#xff0c;這里用C實現了一個輕量的gbk和utf8互相轉換&#xff0c;可跨平臺使用。&#xff08;重量級的可以用libiconv庫&#xff09; 在windows下用<windows.h>頭文件里的函數進行多字節和寬字符轉換&#xff0c;…

mysql5.7初始密碼查看及密碼重置

查看初始密碼 grep temporary password /var/log/mysqld.logcat /root/.mysql_secret mysql密碼找回 密碼重置 vi /etc/my.cnf 在[mysqld]下加上 skip-grant-tables&#xff0c;如&#xff1a; [mysqld] datadir/var/lib/mysql socket/var/lib/mysql/mysql.sock skip-g…