MySQL之單表查詢、多表查詢

1、單表查詢:

單個表的查詢方法及語法順序須要經過實際例子來熟悉mysql

先將表數據建立下:sql

mysql> create database singe_t1;   # 建個數據庫singe_t1
Query OK, 1 row affected (0.01 sec) mysql> use singe_t1     # 進入數據庫singe_t1
Database changed mysql> create table emp(                 # 建立表emp
    -> id int not null unique auto_increment, -> name varchar(20) not null, -> sex enum('male','female') not null default 'male', -> age int(3) unsigned not null default 28, -> hire_date date not null, -> post varchar(50), -> post_comment varchar(100), -> salary int unsigned, -> office int, -> depart_id int -> ); Query OK, 0 rows affected (0.05 sec) mysql> desc emp;    # 查看建立的表結構 
+--------------+-----------------------+------+-----+---------+----------------+ 
| Field        | Type                  | Null | Key | Default | Extra          | 
+--------------+-----------------------+------+-----+---------+----------------+ 
| id           | int(11)               | NO   | PRI | NULL    | auto_increment | 
| name         | varchar(20)           | NO   |     | NULL    |                | 
| sex          | enum('male','female') | NO   |     | male    |                | 
| age          | int(3) unsigned       | NO   |     | 28      |                | 
| hire_date    | date                  | NO   |     | NULL    |                | 
| post         | varchar(50)           | YES  |     | NULL    |                | 
| post_comment | varchar(100)          | YES  |     | NULL    |                | 
| salary       | int(10) unsigned      | YES  |     | NULL    |                | 
| office       | int(11)               | YES  |     | NULL    |                | 
| depart_id    | int(11)               | YES  |     | NULL    |                | 
+--------------+-----------------------+------+-----+---------+----------------+ 
# 開始插入表記錄
mysql> insert into emp(name,sex,age,hire_date,post,salary,office,d -> ('jason','male',18,'20170301','外交部',6600,401,1), #如下是教學部 
    -> ('egon','male',78,'20150302','教學部',1000000,401,1), -> ('kevin','male',81,'20130305','教學部',8300,401,1), -> ('tank','male',73,'20140701','教學部',3500,401,1), -> ('owen','male',28,'20121101','教學部',2100,401,1), -> ('jerry','female',18,'20110211','教學部',9000,401,1), -> ('nick','male',18,'19000301','教學部',30000,401,1), -> ('sean','male',48,'20101111','教學部',10000,401,1), ->                                                            
    -> ('歪歪','female',48,'20150311','銷售部',3000,402,2),#如下是銷售部門 
    -> ('丫丫','female',38,'20101101','銷售部',2000,402,2), -> ('丁丁','female',18,'20110312','銷售部',1000,402,2), -> ('星星','female',18,'20160513','銷售部',3000,402,2), -> ('格格','female',28,'20170127','銷售部',4000,402,2), ->                                                            
    -> ('張野','male',28,'20160311','運營部',10000,403,3), #如下是運營部門 
    -> ('程咬金','male',18,'19970312','運營部',20000,403,3), -> ('程咬銀','female',18,'20130311','運營部',19000,403,3), -> ('程咬銅','male',18,'20150411','運營部',18000,403,3), -> ('程咬鐵','female',18,'20140512','運營部',17000,403,3) -> ; Query OK, 18 rows affected (0.01 sec) Records: 18 Duplicates: 0 Warnings: 0 # 最終結果:
mysql> select * from emp; +----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+  
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id |  
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+  
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |     
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |     
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |     
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |     
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |     
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |     
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |     
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |     
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |       
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |       
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |       
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |       
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |       
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |       
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |        
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |        
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |        
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |        
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
View Code

①、基本語法執行順序

# 最基本的查詢語句:
mysql> select * from emp where id >= 3 and id <= 6; +----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+   
| id | name  | sex    | age | hire_date  | post      | post_comment | salary | office | depart_id |   
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+   
|  3 | kevin | male   |  81 | 2013-03-05 | 教學部    | NULL         |   8300 |    401 |         1 |      
|  4 | tank  | male   |  73 | 2014-07-01 | 教學部    | NULL         |   3500 |    401 |         1 |      
|  5 | owen  | male   |  28 | 2012-11-01 | 教學部    | NULL         |   2100 |    401 |         1 |      
|  6 | jerry | female |  18 | 2011-02-11 | 教學部    | NULL         |   9000 |    401 |         1 |      
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+   
# 基本語句語法的解析:(前後順序) from------------> where----------------> select 找到表 查詢數據的條件依據 找到數據造成虛擬表

②、where約束條件的使用

# 1.查詢id大於等於3小於等於6的數據 mysql> select * from emp where id >= 3 and id <= 6; mysql> select * from emp where id between 3 and 6; +----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+ 
| id | name  | sex    | age | hire_date  | post      | post_comment | salary | office | depart_id | 
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+ 
|  3 | kevin | male   |  81 | 2013-03-05 | 教學部    | NULL         |   8300 |    401 |         1 |    
|  4 | tank  | male   |  73 | 2014-07-01 | 教學部    | NULL         |   3500 |    401 |         1 |    
|  5 | owen  | male   |  28 | 2012-11-01 | 教學部    | NULL         |   2100 |    401 |         1 |    
|  6 | jerry | female |  18 | 2011-02-11 | 教學部    | NULL         |   9000 |    401 |         1 |    
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+ 
# 2.查詢薪資是20000或者18000或者17000的數據 mysql> select * from emp where salary in (20000,18000,17000); mysql> select * from emp where salary=20000 or salary=18000 or salary=17000; +----+-----------+--------+-----+------------+-----------+--------------+--------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+--------+--------+-----------+
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |  20000 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |  18000 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |  17000 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+--------+--------+-----------+
# 3.查詢員工姓名中包含o字母的員工姓名和薪資 mysql> select name,salary from emp where name like '%o%'; +-------+---------+
| name  | salary  |
+-------+---------+
| jason |    6600 |
| egon  | 1000000 |
| owen  |    2100 |
+-------+---------+
# 4.查詢員工姓名是由四個字符組成的員工姓名與其薪資 mysql> select name,salary from emp where name like '____'; # like語句裏面下劃線表明單個通配字符 mysql> select name,salary from emp where char_length(name)=4; # 或者經過計算name字段的長度爲4來查詢
+------+---------+
| name | salary  |
+------+---------+
| egon | 1000000 |
| tank |    3500 |
| owen |    2100 |
| nick |   30000 |
| sean |   10000 |
+------+---------+
# 5.查詢id小於3或者大於6的數據 mysql> select * from emp where id not between 3 and 6; # 運用between語句 mysql> select * from emp where id < 3 or id >6; # 運用or語句
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id | 
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |    
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |    
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |    
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |    
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |      
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |      
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |      
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |      
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |      
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |      
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |       
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |       
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |       
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |       
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
# 6.查詢薪資不在20000,18000,17000範圍的數據 mysql> select * from emp where salary not in (20000,18000,17000); +----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |   
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |   
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |   
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |   
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |   
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |   
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |   
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |   
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |     
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |     
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |     
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |     
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |     
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |     
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |      
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
# 7.查詢崗位描述爲空的員工名與崗位名 針對null不能用等號,只能用is mysql> select name,post from emp where post_comment is null; +-----------+-----------+   
| name      | post      |   
+-----------+-----------+   
| jason     | 外交部    |      
| egon      | 教學部    |      
| kevin     | 教學部    |      
| tank      | 教學部    |      
| owen      | 教學部    |      
| jerry     | 教學部    |      
| nick      | 教學部    |      
| sean      | 教學部    |      
| 歪歪      | 銷售部    |        
| 丫丫      | 銷售部    |        
| 丁丁      | 銷售部    |        
| 星星      | 銷售部    |        
| 格格      | 銷售部    |        
| 張野      | 運營部    |        
| 程咬金    | 運營部    |         
| 程咬銀    | 運營部    |         
| 程咬銅    | 運營部    |         
| 程咬鐵    | 運營部    |         
+-----------+-----------+

③、group by 分組

# 數據有時候都有類似性,這個類似性有時候就是咱們查詢同一類數據的依據,大部分狀況下,數據的類似能夠說就是數據分組的意義所在,好比每一個部門,男女、每一個地方、、、等等。 # 按部門分組 mysql> select * from emp group by post;  +----+--------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
| id | name   | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id | 
+----+--------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
|  1 | jason  | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |    
|  2 | egon   | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |    
| 14 | 張野   | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |      
|  9 | 歪歪   | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |      
+----+--------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ mysql> select id,name,sex from emp group by post; +----+--------+--------+
| id | name   | sex    |
+----+--------+--------+
|  1 | jason  | male   |
|  2 | egon   | male   |
| 14 | 張野   | male   |
|  9 | 歪歪   | female |
+----+--------+--------+ 能夠看出以上按部門分組取出的數據都是能找到的部門分組數據的第一條,這樣作確定是不合理的,由於我只是要查詢分組的數據,你給我每一個分組的第一條id的數據有何用。 因此須要設置sql_model 爲only_full_group_by,這樣意味着之後但凡分組,只能取到分組的依據,不該該去取組裏面的單個元素的值。 mysql> set global sql_mode='strict_trans_tables,only_full_group_by'; # 設置分組嚴格模式 mysql> exit # 退出才能生效 Bye mysql -uroot -p Enter password: ****** ...... mysql> use singe_t1 Database changed mysql> show variables like '%sql_mode%'; # 查看是否生效
+---------------+----------------------------------------+  
| Variable_name | Value                                  |  
+---------------+----------------------------------------+  
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES |  
+---------------+----------------------------------------+  
# 此時若是按照之前的分組查詢方式就會報錯
mysql> select * from emp group by post; ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'singe_t1.emp.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by # 必須 按分組查詢只顯示post字段名才行: mysql> select post from emp group by post; +-----------+                                
| post      |                                
+-----------+                                
| 外交部    |                                   
| 教學部    |                                   
| 運營部    |                                   
| 銷售部    |                                   
+-----------+   
 # 強調:只要分組了,就不可以再「直接」查找到單個數據信息了,只能獲取到組名
------------------------------------------------------------------------------- 接下來開始用實例來熟悉分組的運用: # 2.獲取每一個部門的最高工資 # 以組爲單位統計組內數據>>>聚合查詢(彙集到一塊兒合成爲一個結果) # 每一個部門的最高工資 mysql> select post,max(salary) from emp group by post; +-----------+-------------+
| post      | max(salary) |
+-----------+-------------+
| 外交部    |        6600 |
| 教學部    |     1000000 |
| 運營部    |       20000 |
| 銷售部    |        4000 |
+-----------+-------------+
# 每一個部門的最低工資 mysql> select post,min(salary) from emp group by post; +-----------+-------------+
| post      | min(salary) |
+-----------+-------------+
| 外交部    |        6600 |
| 教學部    |        2100 |
| 運營部    |       10000 |
| 銷售部    |        1000 |
+-----------+-------------+
# 每一個部門的平均工資 mysql> select post,avg(salary) from emp group by post; +-----------+-------------+
| post      | avg(salary) |
+-----------+-------------+
| 外交部    |   6600.0000 |
| 教學部    | 151842.8571 |
| 運營部    |  16800.0000 |
| 銷售部    |   2600.0000 |
+-----------+-------------+
# 每一個部門的工資總和 mysql> select post,sum(salary) from emp group by post; +-----------+-------------+                                 
| post      | sum(salary) |                                 
+-----------+-------------+                                 
| 外交部    |        6600 |                                    
| 教學部    |     1062900 |                                    
| 運營部    |       84000 |                                    
| 銷售部    |       13000 |                                    
+-----------+-------------+                                 
# 每一個部門的人數 mysql> select post,count(id) from emp group by post; +-----------+-----------+
| post      | count(id) |
+-----------+-----------+
| 外交部    |         1 |
| 教學部    |         7 |
| 運營部    |         5 |
| 銷售部    |         5 |
+-----------+-----------+
-------------------------------------------------------------------------------
# 3.查詢分組以後的部門名稱和每一個部門下全部的學生姓名 # group_concat(分組以後用)不只能夠用來顯示除分組外字段還有拼接字符串的做用 mysql> select post,group_concat(name) from emp group by post;  +-----------+------------------------------------------------+                 
| post      | group_concat(name)                             |                 
+-----------+------------------------------------------------+                 
| 外交部    | jason                                          |                    
| 教學部    | egon,kevin,tank,owen,jerry,nick,sean           |                    
| 運營部    | 張野,程咬金,程咬銀,程咬銅,程咬鐵               |                                  
| 銷售部    | 歪歪,丫丫,丁丁,星星,格格                       |                              
+-----------+------------------------------------------------+ mysql> select post,group_concat(name) as 部門所有人員 from emp group by post;  +-----------+------------------------------------------------+                 
| post      | 部門所有人員                                   |                       
+-----------+------------------------------------------------+                 
| 外交部    | jason                                          |                    
| 教學部    | egon,kevin,tank,owen,jerry,nick,sean           |                    
| 運營部    | 張野,程咬金,程咬銀,程咬銅,程咬鐵               |                                  
| 銷售部    | 歪歪,丫丫,丁丁,星星,格格                       |                              
+-----------+------------------------------------------------+ mysql> select post,group_concat(name,'__') from emp group by post; +-----------+----------------------------------------------------------+
| post      | group_concat(name,'__')                                  |
+-----------+----------------------------------------------------------+
| 外交部    | jason__                                                  |
| 教學部    | egon__,kevin__,tank__,owen__,jerry__,nick__,sean__       |
| 運營部    | 張野__,程咬金__,程咬銀__,程咬銅__,程咬鐵__               |
| 銷售部    | 歪歪__,丫丫__,丁丁__,星星__,格格__                       |
+-----------+----------------------------------------------------------+ mysql> select post,group_concat(name,':',salary) from emp group by post; +-----------+------------------------------------------------------------------------------+
| post      | group_concat(name,':',salary)                                                |
+-----------+------------------------------------------------------------------------------+
| 外交部    | jason:6600                                                                   |
| 教學部    | egon:1000000,kevin:8300,tank:3500,owen:2100,jerry:9000,nick:30000,sean:10000 |
| 運營部    | 張野:10000,程咬金:20000,程咬銀:19000,程咬銅:18000,程咬鐵:17000               |
| 銷售部    | 歪歪:3000,丫丫:2000,丁丁:1000,星星:3000,格格:4000                            |
+-----------+------------------------------------------------------------------------------+ mysql> select post,group_concat(salary) as 薪資 from emp group by post; +-----------+-----------------------------------------+                  
| post      | 薪資                                    |                    
+-----------+-----------------------------------------+                  
| 外交部    | 6600                                    |                     
| 教學部    | 1000000,8300,3500,2100,9000,30000,10000 |                     
| 運營部    | 10000,20000,19000,18000,17000           |                     
| 銷售部    | 3000,2000,1000,3000,4000                |                     
+-----------+-----------------------------------------+                  
 ----------------------------------------------------------------------------
# 4.補充concat(不分組時用)拼接字符串達到更好的顯示效果 as語法使用(concat_ws(':',字段1,字段2,字段3)用冒號拼接後面字段) mysql> select name as 姓名,salary as 薪資 from emp where post='教學部';  +--------+---------+                                                    
| 姓名   | 薪資    |                                                        
+--------+---------+                                                    
| egon   | 1000000 |                                                    
| kevin  |    8300 |                                                    
| tank   |    3500 |                                                    
| owen   |    2100 |                                                    
| jerry  |    9000 |                                                    
| nick   |   30000 |                                                    
| sean   |   10000 |                                                    
+--------+---------+ mysql> select concat('姓名:',name) 姓名,concat('工資:',salary) as 薪資 from emp where post='教學部'; +--------------+----------------+
| 姓名         | 薪資           |
+--------------+----------------+
| 姓名:egon    | 工資:1000000   |
| 姓名:kevin   | 工資:8300      |
| 姓名:tank    | 工資:3500      |
| 姓名:owen    | 工資:2100      |
| 姓名:jerry   | 工資:9000      |
| 姓名:nick    | 工資:30000     |
| 姓名:sean    | 工資:10000     |
+--------------+----------------+
 # 補充as語法 便可以給字段起別名也能夠給表起
mysql> create table t2(id int,name char(16));  # 建個t2表
Query OK, 0 rows affected (0.03 sec) mysql> show tables;  # 該數據庫內有2個表
+--------------------+ 
| Tables_in_singe_t1 | 
+--------------------+ 
| emp                | 
| t2                 | 
+--------------------+ mysql> select emp.id,emp.name from emp as t1; # 若是將emp起名爲t1,那麼前面的emp也得改成起名的t1才能找到id和name,不然報錯 ERROR 1054 (42S22): Unknown column 'emp.id' in 'field list' mysql> select t1.id,t1.name from emp as t1 where id between 1 and 6; +----+-------+
| id | name  |
+----+-------+
|  1 | jason |
|  2 | egon  |
|  3 | kevin |
|  4 | tank  |
|  5 | owen  |
|  6 | jerry |
+----+-------+ 小測試:當把表名起名爲庫中已有的表t2呢?是否會報錯? mysql> select t2.id,t2.name from emp as t2 where id between 1 and 6; +----+-------+                                                       
| id | name  |                                                       
+----+-------+                                                       
|  1 | jason |                                                       
|  2 | egon  |                                                       
|  3 | kevin |                                                       
|  4 | tank  |                                                       
|  5 | owen  |                                                       
|  6 | jerry |                                                       
+----+-------+ 結果是不會報錯 分析:查詢表應該是將結果放進內存中而後顯示的,這樣起名只是臨時性的,顯示的結果也是臨時的,因此和數據庫中實際表名沒有關係。只在查詢那段語句中起做用。互不影響。 --------------------------------------------------------------------------------
# 查詢四則運算 # 查詢每一個人的年薪 mysql> select name as 員工,salary*12 as 年薪 from emp where id between 1 and 6;  +--------+----------+                                                            
| 員工   | 年薪     |                                                                
+--------+----------+                                                            
| jason  |    79200 |                                                            
| egon   | 12000000 |                                                            
| kevin  |    99600 |                                                            
| tank   |    42000 |                                                            
| owen   |    25200 |                                                            
| jerry  |   108000 |                                                            
+--------+----------+ 友情提醒:上述的起名語句中的as能夠省略,可是省略後會減弱查詢語句的可讀性,建議仍是不省略好。  

④、having (相似於where,可是必須在group by語句後使用) 

having的語法格式與where一致,只不過having是在分組以後進行的過濾,即where雖然不能用聚合函數,可是having能夠!數據庫

一、統計各部門年齡在30歲以上的員工平均工資,而且保留平均工資大於10000的部門 mysql> select post,avg(salary) from emp where age > 30 group by post having avg(salary)>10000; +-----------+-------------+
| post      | avg(salary) |
+-----------+-------------+
| 教學部    | 255450.0000 |
+-----------+-------------+

⑤、distinct 去重

對有重複的展現數據進行去重操做ide

mysql> create table tt1(id int,name char(16)); mysql> insert into tt1 values (1,'張三'),(2,'王五'),(2,'王五'),(3,'張三'),(1,'趙六'); mysql> select * from tt1; +------+--------+        
| id   | name   |        
+------+--------+        
|    1 | 張三   |          
|    2 | 王五   |          
|    2 | 王五   |          
|    3 | 張三   |          
|    1 | 趙六   |          
+------+--------+ mysql> select distinct * from tt1; +------+--------+
| id   | name   |
+------+--------+
|    1 | 張三   |
|    2 | 王五   |
|    3 | 張三   |
|    1 | 趙六   |
+------+--------+ mysql> select distinct name from tt1; +--------+
| name   |
+--------+
| 張三   |
| 王五   |
| 趙六   |
+--------+ mysql> select distinct id from tt1; +------+                             
| id   |                             
+------+                             
|    1 |                             
|    2 |                             
|    3 |                             
+------+

⑥、order by (排序)

mysql> select * from emp order by salary; # 按照工資升序排序
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id | 
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ 
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |      
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |      
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |    
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |      
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |      
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |    
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |      
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |    
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |    
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |    
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |    
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |      
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |       
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |       
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |       
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |       
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |    
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |    
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+ mysql> select * from emp order by age,salary;    # 首選排序規則爲age,當排完序的age中有相同的時,在相同age的幾個記錄中進行salary排序。 
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+  
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id |  
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+  
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |       
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |       
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |     
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |     
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |        
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |        
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |        
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |        
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |     
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |     
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |       
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |       
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |       
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |       
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |     
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |     
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |     
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |     
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+  
# 默認爲升序,若是想要降序,在後面加上desc。
mysql> select * from emp order by salary desc; +----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+    
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id |    
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+    
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |       
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |       
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |          
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |          
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |          
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |          
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |       
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |         
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |       
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |       
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |       
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |         
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |       
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |         
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |         
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |       
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |         
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |         
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+    
# 來個複雜的: # 統計各部門年齡在25歲以上的員工平均工資,而且保留平均工資大於1000的部門,而後對平均工資進行排序
mysql> select post,avg(salary) from emp -> where age>25 -> group by post -> having avg(salary)>1000 -> order by avg(salary) desc; +-----------+-------------+
| post      | avg(salary) |
+-----------+-------------+
| 教學部    | 204780.0000 |
| 運營部    |  10000.0000 |
| 銷售部    |   3000.0000 |
+-----------+-------------+

 ⑦、limit (限制顯示條數)

# 限制展現條數 limit 參數1 limit 參數1,參數2
mysql> select * from emp limit 5;     # 單個數字5表明從最前面開始顯示5條 
+----+-------+------+-----+------------+-----------+--------------+---------+--------+-----------+  
| id | name  | sex  | age | hire_date  | post      | post_comment | salary  | office | depart_id |  
+----+-------+------+-----+------------+-----------+--------------+---------+--------+-----------+  
|  1 | jason | male |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         1 |     
|  2 | egon  | male |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |     
|  3 | kevin | male |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |     
|  4 | tank  | male |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |     
|  5 | owen  | male |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |     
+----+-------+------+-----+------------+-----------+--------------+---------+--------+-----------+   mysql> select * from emp limit 2,6;    # 從起始行數爲2開始日後顯示6行,這裏不包含其實行數2。 
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+ 
| id | name  | sex    | age | hire_date  | post      | post_comment | salary | office | depart_id | 
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+ 
|  3 | kevin | male   |  81 | 2013-03-05 | 教學部    | NULL         |   8300 |    401 |         1 |    
|  4 | tank  | male   |  73 | 2014-07-01 | 教學部    | NULL         |   3500 |    401 |         1 |    
|  5 | owen  | male   |  28 | 2012-11-01 | 教學部    | NULL         |   2100 |    401 |         1 |    
|  6 | jerry | female |  18 | 2011-02-11 | 教學部    | NULL         |   9000 |    401 |         1 |    
|  7 | nick  | male   |  18 | 1900-03-01 | 教學部    | NULL         |  30000 |    401 |         1 |    
|  8 | sean  | male   |  48 | 2010-11-11 | 教學部    | NULL         |  10000 |    401 |         1 |    
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+

八、正則(沒錯,表查詢也能用正則匹配)函數

# 查詢記錄,其中匹配名字條件爲以j開頭,n或者y結尾的名字
mysql> select * from emp where name regexp '^j.*(n|y)$'; +----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+  
| id | name  | sex    | age | hire_date  | post      | post_comment | salary | office | depart_id |  
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+  
|  1 | jason | male   |  18 | 2017-03-01 | 外交部    | NULL         |   6600 |    401 |         1 |     
|  6 | jerry | female |  18 | 2011-02-11 | 教學部    | NULL         |   9000 |    401 |         1 |     
+----+-------+--------+-----+------------+-----------+--------------+--------+--------+-----------+

2、多表查詢

多個表之間的查詢通常都是在 表之間存在某種邏輯關聯的狀況下進行的查詢,這種邏輯上的關聯其實就是表中某個字段名和另一個表中的字段名存在一個一一對應的關係或者關聯。post

先建立2張表做爲示例 mysql> #建表 
mysql> create table dep( -> id int, -> name varchar(20) -> ); Query OK, 0 rows affected (0.04 sec) mysql> mysql> create table emp( -> id int primary key auto_increment, -> name varchar(20), -> sex enum('male','female') not null default 'male', -> age int, -> dep_id int -> ); Query OK, 0 rows affected (0.04 sec) mysql> #插入數據 
mysql> insert into dep values -> (200,'技術'), -> (201,'人力資源'), -> (202,'銷售'), -> (203,'運營'); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert into emp(name,sex,age,dep_id) values -> ('jason','male',18,200), -> ('egon','female',48,201), -> ('kevin','male',38,201), -> ('nick','female',28,202), -> ('owen','male',18,200), -> ('jerry','female',18,204) -> ; Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> select * from dep; +------+--------------+                                                         
| id   | name         |                                                         
+------+--------------+                                                         
|  200 | 技術         |                                                           
|  201 | 人力資源     |                                                             
|  202 | 銷售         |                                                           
|  203 | 運營         |                                                           
+------+--------------+                                                         
4 rows in set (0.00 sec) mysql> select * from emp; +----+-------+--------+------+--------+                                         
| id | name  | sex    | age  | dep_id |                                         
+----+-------+--------+------+--------+                                         
|  1 | jason | male   |   18 |    200 |                                         
|  2 | egon  | female |   48 |    201 |                                         
|  3 | kevin | male   |   38 |    201 |                                         
|  4 | nick  | female |   28 |    202 |                                         
|  5 | owen  | male   |   18 |    200 |                                         
|  6 | jerry | female |   18 |    204 |                                         
+----+-------+--------+------+--------+                                         
6 rows in set (0.00 sec)

多表查詢示例:測試

# 笛卡爾積 多表查詢
mysql> select * from emp,dep; +----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |  
|  1 | jason | male   |   18 |    200 |  201 | 人力資源     |    
|  1 | jason | male   |   18 |    200 |  202 | 銷售         |  
|  1 | jason | male   |   18 |    200 |  203 | 運營         |  
|  2 | egon  | female |   48 |    201 |  200 | 技術         |  
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |    
|  2 | egon  | female |   48 |    201 |  202 | 銷售         |  
|  2 | egon  | female |   48 |    201 |  203 | 運營         |  
|  3 | kevin | male   |   38 |    201 |  200 | 技術         |  
|  3 | kevin | male   |   38 |    201 |  201 | 人力資源     |    
|  3 | kevin | male   |   38 |    201 |  202 | 銷售         |  
|  3 | kevin | male   |   38 |    201 |  203 | 運營         |  
|  4 | nick  | female |   28 |    202 |  200 | 技術         |  
|  4 | nick  | female |   28 |    202 |  201 | 人力資源     |    
|  4 | nick  | female |   28 |    202 |  202 | 銷售         |  
|  4 | nick  | female |   28 |    202 |  203 | 運營         |  
|  5 | owen  | male   |   18 |    200 |  200 | 技術         |  
|  5 | owen  | male   |   18 |    200 |  201 | 人力資源     |    
|  5 | owen  | male   |   18 |    200 |  202 | 銷售         |  
|  5 | owen  | male   |   18 |    200 |  203 | 運營         |  
|  6 | jerry | female |   18 |    204 |  200 | 技術         |  
|  6 | jerry | female |   18 |    204 |  201 | 人力資源     |    
|  6 | jerry | female |   18 |    204 |  202 | 銷售         |  
|  6 | jerry | female |   18 |    204 |  203 | 運營         |  
+----+-------+--------+------+--------+------+--------------+
# 將全部的數據都對應了一遍進行合併,雖然不合理可是其中有合理的數據,如今咱們須要作的就是找出合理的數據 # 查詢員工及所在部門的信息
mysql> select dep.name,emp.name from emp,dep where emp.dep_id=dep.id; +--------------+-------+
| name         | name  |
+--------------+-------+
| 技術         | jason |
| 人力資源     | egon  |
| 人力資源     | kevin |
| 銷售         | nick  |
| 技術         | owen  |
+--------------+-------+
# 查詢部門爲技術部的員工及部門信息
mysql> select * from emp,dep where emp.dep_id=dep.id and dep.name='技術'; +----+-------+------+------+--------+------+--------+
| id | name  | sex  | age  | dep_id | id   | name   |
+----+-------+------+------+--------+------+--------+
|  1 | jason | male |   18 |    200 |  200 | 技術   |
|  5 | owen  | male |   18 |    200 |  200 | 技術   |
+----+-------+------+------+--------+------+--------+
多表查詢分爲內鏈接、左鏈接、右鏈接、全鏈接,在產生鏈接後 如需跟條件此時就不能用到where,只能使用on做爲替代。 # 將2張表關聯到一塊兒的操做,有專門的方法 # 一、內鏈接(inner join):只取兩張表有對應關係的記錄
mysql> select * from emp inner join dep on emp.dep_id = dep.id; +----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|  3 | kevin | male   |   38 |    201 |  201 | 人力資源     |
|  4 | nick  | female |   28 |    202 |  202 | 銷售         |
|  5 | owen  | male   |   18 |    200 |  200 | 技術         |
+----+-------+--------+------+--------+------+--------------+

# 二、左鏈接(left join):在內鏈接的基礎上保留左表知足條件的所有內容,右表沒有對應上的內容用null表示
mysql> select * from emp left join dep on emp.dep_id = dep.id; +----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |
|  5 | owen  | male   |   18 |    200 |  200 | 技術         |
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|  3 | kevin | male   |   38 |    201 |  201 | 人力資源     |
|  4 | nick  | female |   28 |    202 |  202 | 銷售         |
|  6 | jerry | female |   18 |    204 | NULL | NULL         |
+----+-------+--------+------+--------+------+--------------+
# 3 右鏈接(right join):在內鏈接的基礎上,保留右表知足條件的所有內容,左表沒有對應上的內容用null表示
mysql> select * from emp right join dep on emp.dep_id = dep.id; +------+-------+--------+------+--------+------+--------------+
| id   | name  | sex    | age  | dep_id | id   | name         |
+------+-------+--------+------+--------+------+--------------+
|    1 | jason | male   |   18 |    200 |  200 | 技術         |
|    2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|    3 | kevin | male   |   38 |    201 |  201 | 人力資源     |
|    4 | nick  | female |   28 |    202 |  202 | 銷售         |
|    5 | owen  | male   |   18 |    200 |  200 | 技術         |
| NULL | NULL  | NULL   | NULL |   NULL |  203 | 運營         |
+------+-------+--------+------+--------+------+--------------+
# 4 全鏈接(用union將左鏈接和右鏈接聯合起來):在內鏈接的基礎上保留左右表沒有對應
mysql> 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 | jason | male   |   18 |    200 |  200 | 技術         |
|    5 | owen  | male   |   18 |    200 |  200 | 技術         |
|    2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|    3 | kevin | male   |   38 |    201 |  201 | 人力資源     |
|    4 | nick  | female |   28 |    202 |  202 | 銷售         |
|    6 | jerry | female |   18 |    204 | NULL | NULL         |
| NULL | NULL  | NULL   | NULL |   NULL |  203 | 運營         |
+------+-------+--------+------+--------+------+--------------+

3、子查詢:this

# 就是將一個查詢語句的結果用括號括起來當作另外一個查詢語句的條件去用spa

# 接着上面的表:
mysql> select * from emp; +----+-------+--------+------+--------+
| id | name  | sex    | age  | dep_id |
+----+-------+--------+------+--------+
|  1 | jason | male   |   18 |    200 |
|  2 | egon  | female |   48 |    201 |
|  3 | kevin | male   |   38 |    201 |
|  4 | nick  | female |   28 |    202 |
|  5 | owen  | male   |   18 |    200 |
|  6 | jerry | female |   18 |    204 |
+----+-------+--------+------+--------+ mysql> select * from dep; +------+--------------+
| id   | name         |
+------+--------------+
|  200 | 技術         |
|  201 | 人力資源     |
|  202 | 銷售         |
|  203 | 運營         |
+------+--------------+
# 1.查詢部門是技術或者人力資源的員工信息
mysql> # 先將技術和人力資源部門的id號先找出來:
mysql> select id from dep where name='技術' or name='人力資源'; +------+
| id   |
+------+
|  200 |
|  201 |
+------+ mysql> # 在將上述查詢到的id號做爲條件,進行再一次查詢:
mysql> select * from emp where dep_id in (select id from dep where name='技術' or name='人力資源'); +----+-------+--------+------+--------+
| id | name  | sex    | age  | dep_id |
+----+-------+--------+------+--------+
|  1 | jason | male   |   18 |    200 |
|  2 | egon  | female |   48 |    201 |
|  3 | kevin | male   |   38 |    201 |
|  5 | owen  | male   |   18 |    200 |
+----+-------+--------+------+--------+
--------------------------------------------------------------------------------------------------------------------------------
# 2.每一個部門最新入職的員工
有這張表emp: +----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary  | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+
|  1 | jason     | male   |  18 | 2017-03-01 | 外交部    | NULL         |    6600 |    401 |         0 |
|  2 | egon      | male   |  78 | 2015-03-02 | 教學部    | NULL         | 1000000 |    401 |         1 |
|  3 | kevin     | male   |  81 | 2013-03-05 | 教學部    | NULL         |    8300 |    401 |         1 |
|  4 | tank      | male   |  73 | 2014-07-01 | 教學部    | NULL         |    3500 |    401 |         1 |
|  5 | owen      | male   |  28 | 2012-11-01 | 教學部    | NULL         |    2100 |    401 |         1 |
|  6 | jerry     | female |  18 | 2011-02-11 | 教學部    | NULL         |    9000 |    401 |         1 |
|  7 | nick      | male   |  18 | 1900-03-01 | 教學部    | NULL         |   30000 |    401 |         1 |
|  8 | sean      | male   |  48 | 2010-11-11 | 教學部    | NULL         |   10000 |    401 |         1 |
|  9 | 歪歪      | female |  48 | 2015-03-11 | 銷售部    | NULL         |    3000 |    402 |         2 |
| 10 | 丫丫      | female |  38 | 2010-11-01 | 銷售部    | NULL         |    2000 |    402 |         2 |
| 11 | 丁丁      | female |  18 | 2011-03-12 | 銷售部    | NULL         |    1000 |    402 |         2 |
| 12 | 星星      | female |  18 | 2016-05-13 | 銷售部    | NULL         |    3000 |    402 |         2 |
| 13 | 格格      | female |  28 | 2017-01-27 | 銷售部    | NULL         |    4000 |    402 |         2 |
| 14 | 張野      | male   |  28 | 2016-03-11 | 運營部    | NULL         |   10000 |    403 |         3 |
| 15 | 程咬金    | male   |  18 | 1997-03-12 | 運營部    | NULL         |   20000 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | 運營部    | NULL         |   19000 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | 運營部    | NULL         |   18000 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | 運營部    | NULL         |   17000 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+---------+--------+-----------+

# ①先將入職日期最大的員工查詢處理起名爲表t2 # (select post,max(hire_date) as max_date from emp group by post) as t2 # ②爲了易讀性,將emp表起名爲t1,將須要查詢的結果字段名寫在select後(此時包括t2中的字段,由於select是最後執行的語句) # ③ 用內鏈接將t1和t2鏈接起來,經過t1.hire_date=t2.max_date
最終結果: mysql> select t1.id,t1.name,t1.sex,t1.age,t1.salary,t1.office,t2.* from emp as t1 ->  inner join (select post,max(hire_date) as max_date from emp group by post) as t2 where ->  t1.hire_date=t2.max_date; +----+--------+--------+-----+---------+--------+-----------+------------+
| id | name   | sex    | age | salary  | office | post      | max_date   |
+----+--------+--------+-----+---------+--------+-----------+------------+
|  1 | jason  | male   |  18 |    6600 |    401 | 外交部    | 2017-03-01 |
|  2 | egon   | male   |  78 | 1000000 |    401 | 教學部    | 2015-03-02 |
| 13 | 格格   | female |  28 |    4000 |    402 | 銷售部    | 2017-01-27 |
| 14 | 張野   | male   |  28 |   10000 |    403 | 運營部    | 2016-03-11 |
+----+--------+--------+-----+---------+--------+-----------+------------+ 
記住一個規律,表的查詢結果能夠做爲其餘表的查詢條件,也能夠經過其別名的方式把它做爲一張虛擬表去跟其餘表作關聯查詢

額外題:code

部門中薪資超過部門平均薪資的員工姓名及薪資

mysql> select t1.name,t1.salary,t1.post,t2.avg_salary from emp t1 ->  inner join (select post,avg(salary) as avg_salary from emp group by post) as t2 ->  where t1.post=t2.post and t1.salary>t2.avg_salary; +-----------+---------+-----------+-------------+
| name      | salary  | post      | avg_salary  |
+-----------+---------+-----------+-------------+
| egon      | 1000000 | 教學部    | 151842.8571 |
| 歪歪      |    3000 | 銷售部    |   2600.0000 |
| 星星      |    3000 | 銷售部    |   2600.0000 |
| 格格      |    4000 | 銷售部    |   2600.0000 |
| 程咬金    |   20000 | 運營部    |  16800.0000 |
| 程咬銀    |   19000 | 運營部    |  16800.0000 |
| 程咬銅    |   18000 | 運營部    |  16800.0000 |
| 程咬鐵    |   17000 | 運營部    |  16800.0000 |
+-----------+---------+-----------+-------------+
相關文章
相關標籤/搜索