單表查詢,如下面這個表爲例:
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龍 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 張野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
| 16 | 程咬銀 | female | 18 | 2013-03-11 | operation | NULL | 19000 | 403 | 3 |
| 17 | 程咬銅 | male | 18 | 2015-04-11 | operation | NULL | 18000 | 403 | 3 |
| 18 | 程咬鐵 | female | 18 | 2014-05-12 | operation | NULL | 17000 | 403 | 3 |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
select distinct 字段1,字段2,字段3,... from 庫名.表名 #distinct 去重複功能
where約束條件
group by 按照分組依據,分組查詢
having 分組後的過濾條件(針對組級別的過濾)
order by 排序依據
limit n; 限制顯示的條目
語法:
1.注意先切換路徑use db8,找到文件、打開文件、for循環多是多讀多行才發送給服務端,這樣會減小IO
select * from db8.emp
from db8.emp
2.優先級順序
from打開文件(把表從硬盤讀入內存) ——>where條件過濾——>group by分組
執行順序——————>————————>
group by:聚合函數(avg、sum、max、min等)只能在分完組以後才能使用
若是不寫,其實默認是有where、group by分組的
例如:
select max(salary) from emp;
+-------------+
| max(salary) |
+-------------+
| 1000000.31 |
+-------------+
distinct:去的是記錄的重複
select distinct sex from emp; 去除篩選的重複的sex
+--------+
| sex |
+--------+
| male |
| female |
+--------+
where 過濾:
1.between..and (包含=兩邊的)
select id,name from emp where id between 3 and 6; # id>=3 and id<=6
not between 3 and 6 #能夠取反
2.in
<select * from emp where salary =20000 or salary = 18000 or salary=17000;>
select * from emp where salary in(17000,18000,20000); #最終版(取代連續or的條件)
not in #能夠取反
3.like: _表明任意單個字符 %:表明任意無窮個字符
要求:查找emp裏面含有‘i’字母的員工姓名與其薪資
select name,salary from emp where name like '%i%';
要求:查詢員工姓名由四個字符組成的員工姓名與其薪資
select name,salary from emp where name like '____';
要求:查詢員工職位描述post_comment爲空姓名與職位: (判斷NULL只能用is,不能用=)
select name,post from emp where post_comment is NULL;
is not NULL #取反
group by分組: 聚合函數(avg、sum、max、min等)只能在分完組以後才能使用
什麼是分組:找重複度高的,即有大量重複的字段去使用(id、name重複度過低,不能使用group by)
爲什麼要分組:當咱們要以組爲單位進行統計時就必須分組
補充:#設置sql_mode爲only_full_group_by,一位着之後分組,只能取分組的依據
查看sql_mode:
1.show variables like 'sql_mode';
+---------------+---------------------+
| Variable_name | Value |
+---------------+---------------------+
| sql_mode | STRICT_TRANS_TABLES |
+---------------+---------------------+
2.set global sql_mode='strict_trans_tables,only_full_group_by' ; 這個嚴格模式設置完以後之後就只能取組名,單 單獨分組後的內容是看不到的
即只能取:select post from emp group by post; (name,id。。等都取不到),說明分組是爲了總體來用,把數據彙集在一塊兒,不是爲 了單獨取某條數據
3.select @@sql_mode #查看是否更改爲功
1.max求每一個部門最高工資(select * from emp group by post;分完組以後默認*取出的是每一個組的第一我的)
分組目的是以組爲單位作一個總體的聚合,拿到聚合以後的結果
select post,max(salary) from emp group by post;
取每一個部門最低工資:
select post,min(salary) from emp group by post;
取部門的平均工資:
select post,avg(salary) from emp group by post;
取部門的工資總和:
select post,sum (salary) from emp group by post;
統計每一個部門個數:(按照id)
select post,count(id) from emp group by post;
having by:過濾條件,能夠用聚合函數,由於是在分組以後
where分組以前作的過濾
having 分組以後專門對聚合的結果作進一步篩選
select age from emp group by age having age > 28;
group_concat字符串拼接:
查看全部部門和對應的員工名字:
select post,group_concat(name) from emp group by post;
+----------------------------+-------------------------------------------------------+
| post | group_concat(name) |
+----------------------------+-------------------------------------------------------+
| operation | 程咬鐵,程咬銅,程咬銀,程咬金,張野 |
| sale | 格格,星星,丁丁,丫丫,歪歪 |
| teacher | 成龍,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex |
| 老男孩駐沙河辦事處外交大使 | egon |
+----------------------------+-------------------------------------------------------+
select post,group_concat(name,'_perfect') from emp group by post; #實質就是一個字符串的憑藉
select post,group_concat(salary) from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;
練習:
1.查詢崗位名以及包含的全部員工的名字:
select post,group_concat(name) from emp group by post;
2.查詢崗位名已經崗位內包含的員工個數
select post,count(id) from emp group by post;
3.全部男員工、女員工的個數:
select sex,count(id) from emp group by sex;
4.取部門的平均工資:
select post,avg(salary) from emp group by post;
5.查詢男員工與女員工的平均薪資
select sex,avg(salary) from emp group by sex;
6.統計各部門年齡在30歲以上的員工平均工資:(分析表的結果包含:部門、平均薪資)
select post,avg(salary) from emp where age >= 30 group by post;
+---------+---------------+
| post | avg(salary) |
+---------+---------------+
| sale | 2500.240000 |
| teacher | 255450.077500 |
+---------+---------------+
補充concat不分組時用
select name as 姓名,salary as 薪資 from emp;
select concat('Name:',name) as 姓名,concat('Sal:',salary) as 薪資 from emp;
+-----------------+----------------+
| 姓名 | 薪資 |
+-----------------+----------------+
| Name:egon | Sal:7300.33 |
| Name:alex | Sal:1000000.31 |
| Name:wupeiqi | Sal:8300.00 |
| Name:yuanhao | Sal:3500.00 |
| Name:liwenzhou | Sal:2100.00 |
| Name:jingliyang | Sal:9000.00 |
| Name:jinxin | Sal:30000.00 |
| Name:成龍 | Sal:10000.00 |
| Name:歪歪 | Sal:3000.13 |
| Name:丫丫 | Sal:2000.35 |
| Name:丁丁 | Sal:1000.37 |
| Name:星星 | Sal:3000.29 |
| Name:格格 | Sal:4000.33 |
| Name:張野 | Sal:10000.13 |
| Name:程咬金 | Sal:20000.00 |
| Name:程咬銀 | Sal:19000.00 |
| Name:程咬銅 | Sal:18000.00 |
| Name:程咬鐵 | Sal:17000.00 |
+-----------------+----------------+
select concat(name,':',age,':',sex) from emp; #正常狀況每一個字段間都要輸入:
select concat_ws(':',name,age,sex) from emp; #concat_ws就是須要在多個表中間要加‘:’時,只須要在最前面加‘:’就自動會在多個字段加
mysql> select concat_ws(':',name,age,
sex) from emp;
+-----------------------------+
| concat_ws(':',name,age,sex) |
+-----------------------------+
| egon:18:male |
| alex:78:male |
| wupeiqi:81:male |
| yuanhao:73:male |
| liwenzhou:28:male |
| jingliyang:18:female |
| jinxin:18:male |
| 成龍:48:male |
| 歪歪:48:female |
| 丫丫:38:female |
| 丁丁:18:female |
| 星星:18:female |
| 格格:28:female |
| 張野:28:male |
| 程咬金:18:male |
| 程咬銀:18:female |
| 程咬銅:18:male |
| 程咬鐵:18:female |
+-----------------------------+
四則運算:
計算年薪:
select name,salary*12 as annual_salary from emp; #默承認以不寫as
+------------+---------------+
| name | annual_salary |
+------------+---------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龍 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 張野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬銀 | 228000.00 |
| 程咬銅 | 216000.00 |
| 程咬鐵 | 204000.00 |
+------------+---------------+
select * from emp; #正常狀況
select * from emp as t1; #正常狀況
select empoyee.* from emp as empoyee; #若是要是表名.* 後面必須就要一致,說明 as empoyee先運行
order by排序:
升序:
select age from emp order by age; #select age from emp order by age asc; 默認是升序後面有asc
降序:
select age from emp order by age desc;
limit限制顯示的條件:
select * from emp limit 4; #打印只會顯示前面4條信息
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
+----+---------+------+-----+------------+----------------------------+--------------+------------+--------+-----------+
limit分頁顯示:
select * from emp limit 0,5; #從0開始(不包含0)日後取5條,會打印1-5
select * from emp limit 5,5; #從5開始日後取5條,會打印6-10
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龍 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
正則表達式:
select * from emp where name regexp '^jin.*(g|n)$'; #jin開頭,.*中間能夠是任意字符,g/n結尾的名字
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
+----+------------+--------+-----+------------+---------+--------------+--------+--------+-----------+
多表查詢:
emp:
+-----+----------+ +----+--------
| 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 |
+----+------------+--------+-----+--------+ +-----+----------+
dep:
| id | name |
+-----+----------+
| 200 | 技術 |
| 201 | 人力資源 |
| 202 | 銷售 |
| 203 | 運營 |
select * from emp,dep 同時查詢2張表:(本質是一份數據,員工部門表)
原理:左邊的一天記錄會完整的把右邊的記錄對應一遍:
+----+------------+--------+-----+--------+-----+----------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+-----+--------+-----+----------+
| 1 | egon | male | 18 | 200 | 200 | 技術 | egon完整與部門表對應一次
| 1 | egon | male | 18 | 200 | 201 | 人力資源 |
| 1 | egon | male | 18 | 200 | 202 | 銷售 |
| 1 | egon | male | 18 | 200 | 203 | 運營 |
| 2 | alex | female | 48 | 201 | 200 | 技術 | alex完整與部門表對應一次
| 2 | alex | female | 48 | 201 | 201 | 人力資源 |
| 2 | alex | female | 48 | 201 | 202 | 銷售 |
| 2 | alex | female | 48 | 201 | 203 | 運營 |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技術 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 銷售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 運營 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技術 |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力資源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 銷售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 運營 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術 |
| 5 | liwenzhou | male | 18 | 200 | 201 | 人力資源 |
| 5 | liwenzhou | male | 18 | 200 | 202 | 銷售 |
| 5 | liwenzhou | male | 18 | 200 | 203 | 運營 |
| 6 | jingliyang | female | 18 | 204 | 200 | 技術 |
| 6 | jingliyang | female | 18 | 204 | 201 | 人力資源 |
| 6 | jingliyang | female | 18 | 204 | 202 | 銷售 |
| 6 | jingliyang | female | 18 | 204 | 203 | 運營 |
+----+------------+--------+-----+--------+-----+----------+
笛卡爾積
彙總員工表emp裏面對應的部門信息:
select * from emp,dep where emp.dep_id = dep.id; (基礎寫法)
+-----+----------+----+-----------+--------+-----+--------+
| id | name | id | name | sex | age | dep_id |
+-----+----------+----+-----------+--------+-----+--------+
| 200 | 技術 | 1 | egon | male | 18 | 200 |
| 201 | 人力資源 | 2 | alex | female | 48 | 201 |
| 201 | 人力資源 | 3 | wupeiqi | male | 38 | 201 |
| 202 | 銷售 | 4 | yuanhao | female | 28 | 202 |
| 200 | 技術 | 5 | liwenzhou | male | 18 | 200 |
+-----+----------+----+-----------+--------+-----+--------+
只打印技術部門的人:
select * from dep,emp where emp.dep_id = dep.id and dep.name='技術';(在基礎上再加and的判斷語句)
+-----+------+----+-----------+------+-----+--------+
| id | name | id | name | sex | age | dep_id |
+-----+------+----+-----------+------+-----+--------+
| 200 | 技術 | 1 | egon | male | 18 | 200 |
| 200 | 技術 | 5 | liwenzhou | male | 18 | 200 |
+-----+------+----+-----------+------+-----+--------+
只查技術部的人名:
select emp.name from emp,dep where emp.dep_id = dep.id and dep.name='技術';
3.一、2條件基礎上對結果近一步篩選 1.笛卡爾積篩選後的基礎表格 2.基於基礎添加判斷進一步篩選
+-----------+
| name |
+-----------+
| egon |
| liwenzhou |
+-----------+
查egon所在的部門名:
select dep.name from emp,dep where emp.dep_id = dep.id and emp.name='egon';
+------+
| name |
+------+
| 技術 |
+------+
1.內鏈接:不推薦
select * from emp,dep where emp.dep_id = dep.id;
2.內鏈接標準寫法:inner join...on:只取兩張表有對應關係的記錄
inner join...on 只取相同部分,與上面原理同樣
select * from emp inner join dep on emp.dep_id = dep.id where dep.name='技術';
思考:emp裏面沒有對應關係的 jingliyang 204沒出來,如何作保留
左鏈接:在內鏈接的基礎上,保留左表沒有對應關係的記錄
select * from emp left joindep on emp.dep_id = dep.id;
+----+------------+--------+-----+--------+------+----------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+-----+--------+------+----------+
| 1 | egon | male | 18 | 200 | 200 | 技術 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術 |
| 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 |
+----+------------+--------+-----+--------+------+----------+
6 rows in set
右鏈接:在內鏈接的基礎上,保留右表沒有對應關係的記錄
select * from emp right join dep on emp.dep_id = dep.id;
+------+-----------+--------+------+--------+-----+----------+
| id | name | sex | age | dep_id | id | name |
+------+-----------+--------+------+--------+-----+----------+
| 1 | egon | male | 18 | 200 | 200 | 技術 |
| 2 | alex | female | 48 | 201 | 201 | 人力資源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 銷售 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術 |
| NULL | NULL | NULL | NULL | NULL | 203 | 運營 |
+------+-----------+--------+------+--------+-----+----------+
全鏈接:在內鏈接的基礎上保留右表沒有對應關係的記錄
select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;
+------+------------+--------+------+--------+------+----------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+----------+
| 1 | egon | male | 18 | 200 | 200 | 技術 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技術 |
| 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 | 運營 |
+------+------------+--------+------+--------+------+----------+
工做順序就是:
1.先實現笛卡爾積
2.再看是數據inner join、left join、 right join
虛擬錶鏈接查詢:mysql
環境是隻有一個emp員工信息工資表:
t1:表格
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000 | 401 | 1 |
| 8 | 成龍 | male | 48 | 2010-11-11 | teacher | NULL | 10000 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 張野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
| 16 | 程咬銀 | female | 18 | 2013-03-11 | operation | NULL | 19000 | 403 | 3 |
| 17 | 程咬銅 | male | 18 | 2015-04-11 | operation | NULL | 18000 | 403 | 3 |
| 18 | 程咬鐵 | female | 18 | 2014-05-12 | operation | NULL | 17000 | 403 | 3 |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
t2表格:(虛擬表)
+----------------------------+-------------+
| post | max(salary) |
+----------------------------+-------------+
| operation | 20000 |
| sale | 4000.33 |
| teacher | 1000000.31 |
| 老男孩駐沙河辦事處外交大使 | 7300.33 |
+----------------------------+-------------+
實例2:
1.思考怎麼實現找到薪資最高的人,首先不能直接在emp這張表上直接實現獲得
2.想獲得每一個部門薪資最高的人,其實就是在t2的基礎上作近一步的篩選
3.爲了拿到人名,全部將t1與t2合併,重新表裏再取提取對應的薪資最高的人
將2張表格 inner join
select t1.id,t1.name,t1.salary,t1.post,t2.post,t2.ms
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2 #將t2的max(salary)字段變爲ms ;as t2是將整個t2表格變爲t2
on t1.post = t2.post;
+----+------------+------------+----------------------------+----------------------------+------------+
| id | name | salary | post | post | ms |
+----+------------+------------+----------------------------+----------------------------+------------+
| 1 | egon | 7300.33 | 老男孩駐沙河辦事處外交大使 | 老男孩駐沙河辦事處外交大使 | 7300.33 |
| 2 | alex | 1000000.31 | teacher | teacher | 1000000.31 |
| 3 | wupeiqi | 8300 | teacher | teacher | 1000000.31 | #前面的名字是數據後面部門的,後面的薪資是這個部門的最高工資
| 4 | yuanhao | 3500 | teacher | teacher | 1000000.31 |
| 5 | liwenzhou | 2100 | teacher | teacher | 1000000.31 |
| 6 | jingliyang | 9000 | teacher | teacher | 1000000.31 |
| 7 | jinxin | 30000 | teacher | teacher | 1000000.31 |
| 8 | 成龍 | 10000 | teacher | teacher | 1000000.31 |
| 9 | 歪歪 | 3000.13 | sale | sale | 4000.33 |
| 10 | 丫丫 | 2000.35 | sale | sale | 4000.33 |
| 11 | 丁丁 | 1000.37 | sale | sale | 4000.33 |
| 12 | 星星 | 3000.29 | sale | sale | 4000.33 |
| 13 | 格格 | 4000.33 | sale | sale | 4000.33 |
| 14 | 張野 | 10000.13 | operation | operation | 20000 |
| 15 | 程咬金 | 20000 | operation | operation | 20000 |
| 16 | 程咬銀 | 19000 | operation | operation | 20000 |
| 17 | 程咬銅 | 18000 | operation | operation | 20000 |
| 18 | 程咬鐵 | 17000 | operation | operation | 20000 |
+----+------------+------------+----------------------------+----------------------------+------------+
18 rows in set
連表的目的是:判斷員工薪資是否是部門最高工資 t1.salary = t2.ms
select t1.id,t1.name,t1.salary,t1.post,t2.post,t2.ms
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary=t2.ms;
+----+--------+------------+----------------------------+----------------------------+------------+
| id | name | salary | post | post | ms |
+----+--------+------------+----------------------------+----------------------------+------------+
| 1 | egon | 7300.33 | 老男孩駐沙河辦事處外交大使 | 老男孩駐沙河辦事處外交大使 | 7300.33 |
| 2 | alex | 1000000.31 | teacher | teacher | 1000000.31 |
| 13 | 格格 | 4000.33 | sale | sale | 4000.33 |
| 15 | 程咬金 | 20000 | operation | operation | 20000 |
+----+--------+------------+----------------------------+----------------------------+------------+
最終版:代碼實現拿到薪資最高對應的人
select t1.* #最後要的只是t1表裏面的信息,惟一就是篩選出了最高工資對應的人,(t2實質就是中間過分判斷的依據)
from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary=t2.ms;
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000 | 403 | 3 |
+----+--------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
正則表達式