mysql四-1:單表查詢

1、單表查詢的語法

SELECT 字段1,字段2... FROM 表名
                  WHERE 條件
                  GROUP BY field
                  HAVING 篩選
                  ORDER BY field
                  LIMIT 限制條數

2、關鍵字的執行優先級(重點)

一、from  庫.表——找到表

二、where  條件——按照where指定的約束條件,去表中取出一條條記錄

三、group by  分組條件——對取出的一條條記錄分組,若是沒有group by,總體做爲一組

四、having  過濾——將分組的結果進行過濾

五、select——從虛擬表選擇出須要的內容

六、distinct——去重,若是在查詢中指定了DISTINCT子句,則會建立一張內存臨時表(若是內存放不下,就須要存放在硬盤了)。這張臨時表的表結構和上一步產生的虛擬表VT7是同樣的,不一樣的是對進行DISTINCT操做的列增長了一個惟一索引,以此來除重複數據。

七、order by 排序字段——對虛擬表中的內容按照指定的列進行排序,而後返回一個新的虛擬表

八、limit n;——限制結果的顯示條數,LIMIT子句從上一步獲得的VT8虛擬表中選出從指定位置開始的指定行數據。對於沒有應用ORDER BY的LIMIT子句,獲得的結果一樣是無序的,因此,不少時候,咱們都會看到LIMIT子句會和ORDER BY子句一塊兒使用

詳細見:http://www.cnblogs.com/linhaifeng/articles/7372774.htmlhtml

3、簡單查詢

company.employee
    員工id      id                  int             
    姓名        emp_name            varchar
    性別        sex                 enum
    年齡        age                 int
    入職日期     hire_date           date
    崗位        post                varchar
    職位描述     post_comment        varchar
    薪水        salary              double
    辦公室       office              int
    部門編號     depart_id           int



#建立表
create table employee(
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 double(15,2),
office int, #一個部門一個屋子
depart_id int
);


#查看錶結構
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| 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       | double(15,2)          | YES  |     | NULL    |                |
| office       | int(11)               | YES  |     | NULL    |                |
| depart_id    | int(11)               | YES  |     | NULL    |                |
+--------------+-----------------------+------+-----+---------+----------------+

#插入記錄
#三個部門:教學,銷售,運營
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','老男孩駐沙河辦事處外交大使',7300.33,401,1), #如下是教學部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龍','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',3000.13,402,2),#如下是銷售部門
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('張野','male',28,'20160311','operation',10000.13,403,3), #如下是運營部門
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬銀','female',18,'20130311','operation',19000,403,3),
('程咬銅','male',18,'20150411','operation',18000,403,3),
('程咬鐵','female',18,'20140512','operation',17000,403,3)
;

#ps:若是在windows系統中,插入中文字符,select的結果爲空白,能夠將全部字符編碼統一設置成gbk
準備表和記錄
#簡單查詢
    SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id 
    FROM employee;

    SELECT * FROM employee;

    SELECT name,salary FROM employee;

#避免重複DISTINCT
    SELECT DISTINCT post FROM employee;    

#經過四則運算查詢
    SELECT name, salary*12 FROM employee;
    SELECT name, salary*12 AS Annual_salary FROM employee;
    SELECT name, salary*12 Annual_salary FROM employee;

#定義顯示格式
   CONCAT() 函數用於鏈接字符串
   SELECT CONCAT('姓名: ',name,'  年薪: ', salary*12)  AS Annual_salary 
   FROM employee;
   
   CONCAT_WS() 第一個參數爲分隔符
   SELECT CONCAT_WS(':',name,salary*12)  AS Annual_salary 
   FROM employee;

   結合CASE語句:
   SELECT
       (
           CASE
           WHEN NAME = 'egon' THEN
               NAME
           WHEN NAME = 'alex' THEN
               CONCAT(name,'_BIGSB')
           ELSE
               concat(NAME, 'SB')
           END
       ) as new_name
   FROM
       emp;
mysql> select * from employee;                                                                                                                       +----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 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.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    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.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

mysql> select id,name,salary from employee;
+----+------------+------------+
| id | name       | salary     |
+----+------------+------------+
|  1 | egon       |    7300.33 |
|  2 | alex       | 1000000.31 |
|  3 | wupeiqi    |    8300.00 |
|  4 | yuanhao    |    3500.00 |
|  5 | liwenzhou  |    2100.00 |
|  6 | jingliyang |    9000.00 |
|  7 | jinxin     |   30000.00 |
|  8 | 成龍       |   10000.00 |
|  9 | 歪歪       |    3000.13 |
| 10 | 丫丫       |    2000.35 |
| 11 | 丁丁       |    1000.37 |
| 12 | 星星       |    3000.29 |
| 13 | 格格       |    4000.33 |
| 14 | 張野       |   10000.13 |
| 15 | 程咬金     |   20000.00 |
| 16 | 程咬銀     |   19000.00 |
| 17 | 程咬銅     |   18000.00 |
| 18 | 程咬鐵     |   17000.00 |
+----+------------+------------+
18 rows in set (0.00 sec)
簡單查詢

  在須要去重查詢時,能夠利用distinct去除重複信息。python

# distinct去除重複的職位信息
mysql> select distinct post from employee;
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| 老男孩駐沙河辦事處外交大使              |
| teacher                                 |
| sale                                    |
| operation                               |
+-----------------------------------------+
4 rows in set (0.00 sec)
避免重複DISTINCT

  利用四則運算查詢(加減乘除)獲得須要的查詢結果。針對四則運算的字段還能夠取別名,以優化顯示結果。mysql

# 查看一我的的年薪
mysql> select name, salary*12 from employee;
+------------+-------------+
| name       | salary*12   |
+------------+-------------+
| 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 |
+------------+-------------+
18 rows in set (0.00 sec)

# 給年薪起一個別名
mysql> select name, salary*12 as Annual_salary from employee;
+------------+---------------+
| 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 |
+------------+---------------+
18 rows in set (0.00 sec)

# 起別名不加as的方法
mysql> select name, salary*12 Annual_salary from employee;
+------------+---------------+
| 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 |
+------------+---------------+
18 rows in set (0.00 sec)
四則運算及取別名方法

  利用concat和逗號自由定義顯示格式正則表達式

# concat()函數用於鏈接字符串
mysql> select concat('姓名:',name) from employee;
+------------------------+
| concat('姓名:',name)   |
+------------------------+
| 姓名:egon              |
| 姓名:alex              |
| 姓名:wupeiqi           |
| 姓名:yuanhao           |
| 姓名:liwenzhou         |
| 姓名:jingliyang        |
| 姓名:jinxin            |
| 姓名:成龍              |
| 姓名:歪歪              |
| 姓名:丫丫              |
| 姓名:丁丁              |
| 姓名:星星              |
| 姓名:格格              |
| 姓名:張野              |
| 姓名:程咬金            |
| 姓名:程咬銀            |
| 姓名:程咬銅            |
| 姓名:程咬鐵            |
+------------------------+
18 rows in set (0.00 sec)

mysql> select concat('姓名:',name,'性別:',sex,'年薪:',salary*12) as emp_info_salary from employee;   # as設置查詢結果標題
+------------------------------------------------+
| emp_info_salary                                |
+------------------------------------------------+
| 姓名:egon性別:male年薪:87603.96                |
| 姓名:alex性別:male年薪:12000003.72             |
| 姓名:wupeiqi性別:male年薪:99600.00             |
| 姓名:yuanhao性別:male年薪:42000.00             |
| 姓名:liwenzhou性別:male年薪:25200.00           |
| 姓名:jingliyang性別:female年薪:108000.00       |
| 姓名:jinxin性別:male年薪:360000.00             |
| 姓名:成龍性別:male年薪:120000.00               |
| 姓名:歪歪性別:female年薪:36001.56              |
| 姓名:丫丫性別:female年薪:24004.20              |
| 姓名:丁丁性別:female年薪:12004.44              |
| 姓名:星星性別:female年薪:36003.48              |
| 姓名:格格性別:female年薪:48003.96              |
| 姓名:張野性別:male年薪:120001.56               |
| 姓名:程咬金性別:male年薪:240000.00             |
| 姓名:程咬銀性別:female年薪:228000.00           |
| 姓名:程咬銅性別:male年薪:216000.00             |
| 姓名:程咬鐵性別:female年薪:204000.00           |
+------------------------------------------------+
18 rows in set (0.00 sec)

# 想把薪資單獨分出來,用concat和逗號拼接出想要的格式
mysql> select concat('姓名:',name,'性別:',sex) as info, concat('年薪:', salary*12) as annual_salary from employee;                                   
+--------------------------------+--------------------+
| info                           | annual_salary      |
+--------------------------------+--------------------+
| 姓名:egon性別:male             | 年薪:87603.96      |
| 姓名:alex性別:male             | 年薪:12000003.72   |
| 姓名:wupeiqi性別:male          | 年薪:99600.00      |
| 姓名:yuanhao性別:male          | 年薪:42000.00      |
| 姓名:liwenzhou性別:male        | 年薪:25200.00      |
| 姓名:jingliyang性別:female     | 年薪:108000.00     |
| 姓名:jinxin性別:male           | 年薪:360000.00     |
| 姓名:成龍性別:male             | 年薪:120000.00     |
| 姓名:歪歪性別:female           | 年薪:36001.56      |
| 姓名:丫丫性別:female           | 年薪:24004.20      |
| 姓名:丁丁性別:female           | 年薪:12004.44      |
| 姓名:星星性別:female           | 年薪:36003.48      |
| 姓名:格格性別:female           | 年薪:48003.96      |
| 姓名:張野性別:male             | 年薪:120001.56     |
| 姓名:程咬金性別:male           | 年薪:240000.00     |
| 姓名:程咬銀性別:female         | 年薪:228000.00     |
| 姓名:程咬銅性別:male           | 年薪:216000.00     |
| 姓名:程咬鐵性別:female         | 年薪:204000.00     |
+--------------------------------+--------------------+
18 rows in set (0.00 sec)
concat()函數鏈接字符串
# concat_ws()  第一個參數爲分隔符,處理多條記錄+分隔符更方便
mysql> select concat_ws(':', name, sex, salary) as emp_info from employee;
+---------------------------+
| emp_info                  |
+---------------------------+
| egon:male:7300.33         |
| alex:male:1000000.31      |
| wupeiqi:male:8300.00      |
| yuanhao:male:3500.00      |
| liwenzhou:male:2100.00    |
| jingliyang:female:9000.00 |
| jinxin:male:30000.00      |
| 成龍:male:10000.00        |
| 歪歪:female:3000.13       |
| 丫丫:female:2000.35       |
| 丁丁:female:1000.37       |
| 星星:female:3000.29       |
| 格格:female:4000.33       |
| 張野:male:10000.13        |
| 程咬金:male:20000.00      |
| 程咬銀:female:19000.00    |
| 程咬銅:male:18000.00      |
| 程咬鐵:female:17000.00    |
+---------------------------+
18 rows in set (0.00 sec)
concat_ws()優化處理記錄和分隔符

  結合case語句,自定義複雜的顯示格式。sql

# 結合CASE語句
mysql> SELECT
    -> (
    ->        CASE
    ->        WHEN NAME = 'egon' THEN
    ->            NAME
    ->        WHEN NAME = 'alex' THEN
    ->            CONCAT(name,'_BIGSB')
    ->        ELSE
    ->            concat(NAME, 'SB')
    ->        END
    ->    ) as new_name
    -> FROM
    ->    employee;
+--------------+
| new_name     |
+--------------+
| egon         |
| alex_BIGSB   |
| wupeiqiSB    |
| yuanhaoSB    |
| liwenzhouSB  |
| jingliyangSB |
| jinxinSB     |
| 成龍SB       |
| 歪歪SB       |
| 丫丫SB       |
| 丁丁SB       |
| 星星SB       |
| 格格SB       |
| 張野SB       |
| 程咬金SB     |
| 程咬銀SB     |
| 程咬銅SB     |
| 程咬鐵SB     |
+--------------+
18 rows in set (0.00 sec)
CASE語句自定義查詢結果

小練習:windows

  一、查出全部員工的名字,薪資,格式爲  <名字:egon>  <薪資:3000>緩存

  二、查出全部崗位(去掉重複)ide

  三、查出全部員工名字,以及他們的年薪,年薪字段名爲annual_year函數

mysql> select concat("<名字:",name,">") as NAME, concat("<薪資:",salary,">") as SALARY from employee;          
+---------------------+---------------------+
| NAME                | SALARY              |
+---------------------+---------------------+
| <名字:egon>         | <薪資:7300.33>      |
| <名字:alex>         | <薪資:1000000.31>   |
| <名字:wupeiqi>      | <薪資:8300.00>      |
| <名字:yuanhao>      | <薪資:3500.00>      |
| <名字:liwenzhou>    | <薪資:2100.00>      |
| <名字:jingliyang>   | <薪資:9000.00>      |
| <名字:jinxin>       | <薪資:30000.00>     |
| <名字:成龍>         | <薪資:10000.00>     |
| <名字:歪歪>         | <薪資:3000.13>      |
| <名字:丫丫>         | <薪資:2000.35>      |
| <名字:丁丁>         | <薪資:1000.37>      |
| <名字:星星>         | <薪資:3000.29>      |
| <名字:格格>         | <薪資:4000.33>      |
| <名字:張野>         | <薪資:10000.13>     |
| <名字:程咬金>       | <薪資:20000.00>     |
| <名字:程咬銀>       | <薪資:19000.00>     |
| <名字:程咬銅>       | <薪資:18000.00>     |
| <名字:程咬鐵>       | <薪資:17000.00>     |
+---------------------+---------------------+
18 rows in set (0.00 sec)

mysql> select distinct post from employee;
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| 老男孩駐沙河辦事處外交大使              |
| teacher                                 |
| sale                                    |
| operation                               |
+-----------------------------------------+
4 rows in set (0.00 sec)

mysql> select name, salary*12 as annual_year from employee;
+------------+-------------+
| name       | annual_year |
+------------+-------------+
| 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 |
+------------+-------------+
18 rows in set (0.00 sec)
練習題答案

4、WHERE約束

where字句可使用:post

  一、比較運算符:> <   >=  <= ; <>  != (這兩個不等於符號,通常採用!=)

  二、between 80 and 100 指的就是大於等於80,小於等於100

  三、in(80,90,100) 知足在這個範圍內,值是80或90或100

  四、like 'egon%'  這種是模糊匹配

    pattern能夠是%或_,

    %表示任意多字符;_表示一個字符

  五、邏輯運算符:在多個條件直接可使用邏輯運算符 and(與)  or(或)  not(非)

#1:單條件查詢
    SELECT name FROM employee
        WHERE post='sale';
        
#2:多條件查詢 (AND串聯條件)
    SELECT name,salary FROM employee
        WHERE post='teacher' AND salary>10000;

#3:關鍵字BETWEEN AND
    SELECT name,salary FROM employee 
        WHERE salary BETWEEN 10000 AND 20000;

    SELECT name,salary FROM employee 
        WHERE salary NOT BETWEEN 10000 AND 20000;
    
#4:關鍵字IS NULL(判斷某個字段是否爲NULL不能用等號,須要用IS)
    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NULL;

    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NOT NULL;
        
    SELECT name,post_comment FROM employee 
        WHERE post_comment=''; 注意''是空字符串,不是null
    ps:
        執行
        update employee set post_comment='' where id=2;
        再用上條查看,就會有結果了

#5:關鍵字IN集合查詢
    SELECT name,salary FROM employee 
        WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
    
    SELECT name,salary FROM employee 
        WHERE salary IN (3000,3500,4000,9000) ;

    SELECT name,salary FROM employee 
        WHERE salary NOT IN (3000,3500,4000,9000) ;

#6:關鍵字LIKE模糊查詢
    通配符’%SELECT * FROM employee 
            WHERE name LIKE 'eg%';

    通配符’_’
    SELECT * FROM employee 
            WHERE name LIKE 'al__';
# 1、單條件查詢
mysql> select id,name,age from employee where id > 7;  
+----+-----------+-----+
| id | name      | age |
+----+-----------+-----+
|  8 | 成龍      |  48 |
|  9 | 歪歪      |  48 |
| 10 | 丫丫      |  38 |
| 11 | 丁丁      |  18 |
| 12 | 星星      |  18 |
| 13 | 格格      |  28 |
| 14 | 張野      |  28 |
| 15 | 程咬金    |  18 |
| 16 | 程咬銀    |  18 |
| 17 | 程咬銅    |  18 |
| 18 | 程咬鐵    |  18 |
+----+-----------+-----+
11 rows in set (0.00 sec)

mysql> select name from employee where post='sale';
+--------+
| name   |
+--------+
| 歪歪   |
| 丫丫   |
| 丁丁   |
| 星星   |
| 格格   |
+--------+
5 rows in set (0.00 sec)

# 2、多條件查詢(AND串聯)
mysql> select name,salary from employee 
    -> where post='teacher' and salary>10000;
+--------+------------+
| name   | salary     |
+--------+------------+
| alex   | 1000000.31 |
| jinxin |   30000.00 |
+--------+------------+
2 rows in set (0.00 sec)


mysql> select * from employee where post ='teacher' and salary>8000; 
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex       | male   |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher | NULL         |    8300.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher | NULL         |   10000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
5 rows in set (0.00 sec)


# 3、關鍵字BETWEEN x AND y ————大於等於x,小於等於y
mysql> select name,salary from employee
    -> where salary between 10000 and 20000;
+-----------+----------+
| name      | salary   |
+-----------+----------+
| 成龍      | 10000.00 |
| 張野      | 10000.13 |
| 程咬金    | 20000.00 |
| 程咬銀    | 19000.00 |
| 程咬銅    | 18000.00 |
| 程咬鐵    | 17000.00 |
+-----------+----------+
6 rows in set (0.01 sec)

mysql> select name,salary from employee
    -> where salary NOT BETWEEN 10000 AND 20000;    # 10000如下,20000以上
+------------+------------+
| name       | salary     |
+------------+------------+
| egon       |    7300.33 |
| alex       | 1000000.31 |
| wupeiqi    |    8300.00 |
| yuanhao    |    3500.00 |
| liwenzhou  |    2100.00 |
| jingliyang |    9000.00 |
| jinxin     |   30000.00 |
| 歪歪       |    3000.13 |
| 丫丫       |    2000.35 |
| 丁丁       |    1000.37 |
| 星星       |    3000.29 |
| 格格       |    4000.33 |
+------------+------------+
12 rows in set (0.00 sec)

#4:關鍵字IN集合查詢
mysql> select * from employee where salary=3000 or salary=3500 or salary=4000 or salary=9000; 
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary  | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         | 3500.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         | 9000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
2 rows in set (0.00 sec)

mysql> select * from employee
    -> where salary in (3000,3500,4000,9000);
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary  | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         | 3500.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         | 9000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+---------+--------+-----------+
2 rows in set (0.01 sec)

mysql> select * from employee where salary not in (3000,3500,4000,9000);
+----+-----------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 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.00 |    401 |         1 |
|  5 | liwenzhou | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  7 | jinxin    | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍      | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    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.00 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
16 rows in set (0.00 sec)

#5:關鍵字IS NULL(判斷某個字段是否爲NULL不能用等號,須要用IS)
# 注意NULL和''空字符串是不一樣的
mysql> select * from employee
    -> where post_comment is null;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 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.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    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.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

# is not NULL取反
mysql> select * from employee where post_comment is  not null;
Empty set (0.00 sec)

mysql> select * from employee where post_comment='';  # 注意‘’空字符串不是null
Empty set (0.00 sec)

#6:關鍵字LIKE模糊匹配
mysql> select * from employee 
    -> where name like 'eg%';   # 任意個數任意字符
+----+------+------+-----+------------+-----------------------------------------+--------------+---------+--------+-----------+
| 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 |
+----+------+------+-----+------------+-----------------------------------------+--------------+---------+--------+-----------+
1 row in set (0.01 sec)

mysql> select * from employee
    -> where name like 'al__';   # 一個'_'對應一個任意字符
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec)
where約束操做示例

5、分組查詢:GROUP BY

一、什麼是分組?爲何要分組?

  一、分組發生在where以後,即分組是基於where以後獲得的記錄而進行的

  二、分組指的是:將全部記錄按照某個相同字段進行歸類,好比針對員工信息表的職位分組,或者按照性別進行分組等。

  三、爲何要分組:分門別類地進行處理。

          每一個部門員工、男人與女人數等,「每」這個字後面的字段,就是咱們的分組依據。

  四、大前提:能夠按照任意字段分組,可是分組完畢後,好比group by post,只能查看post字段,若是想查看組內信息,須要藉助於聚合函數。

二、ONLY_FULL_GROUP_BY

#查看MySQL 5.7默認的sql_mode以下:
mysql> select @@global.sql_mode;
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

#!!!注意
ONLY_FULL_GROUP_BY的語義就是肯定select target list中的全部列的值都是明確語義,簡單的說來,在ONLY_FULL_GROUP_BY模式下,
target list中的值要麼是來自於彙集函數的結果,要麼是來自於group
by list中的表達式的值。 #設置sql_mole以下操做(咱們能夠去掉ONLY_FULL_GROUP_BY模式): mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

  在沒有設置ONLY_FULL_GROUP_BY時,在分組的狀況下查看到其餘字段的信息,也能夠有結果,默認都是組內的第一條記錄。可是這個的意義不大。

mysql> select @@global.sql_mode;
+-------------------+
| @@global.sql_mode |
+-------------------+
|                   |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from emp group by post; 
+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name | sex    | age | hire_date  | post                       | post_comment | salary     | office | depart_id |
+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 14 | 張野 | male   |  28 | 2016-03-11 | operation                  | NULL         |   10000.13 |    403 |         3 |
|  9 | 歪歪 | female |  48 | 2015-03-11 | sale                       | NULL         |    3000.13 |    402 |         2 |
|  2 | alex | male   |  78 | 2015-03-02 | teacher                    | NULL         | 1000000.31 |    401 |         1 |
|  1 | egon | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使 | NULL         |    7300.33 |    401 |         1 |
+----+------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
4 rows in set (0.00 sec)


#因爲沒有設置ONLY_FULL_GROUP_BY,因而也能夠有結果,默認都是組內的第一條記錄,但其實這是沒有意義的

mysql> set global sql_mode='ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected (0.00 sec)

mysql> quit #設置成功後,必定要退出,而後從新登陸方可生效
Bye

mysql> use db1;
Database changed
mysql> select * from emp group by post; #報錯
ERROR 1055 (42000): 'db1.emp.id' isn't in GROUP BY
mysql> select post,count(id) from emp group by post; #只能查看分組依據和使用聚合函數
+----------------------------+-----------+
| post                       | count(id) |
+----------------------------+-----------+
| operation                  |         5 |
| sale                       |         5 |
| teacher                    |         7 |
| 老男孩駐沙河辦事處外交大使 |         1 |
+----------------------------+-----------+
4 rows in set (0.00 sec)

三、GROUP BY

(1)單獨使用GROUP BY關鍵字分組:

  SELECT post FROM employee GROUP BY post;

  注意:咱們按照post字段分組,那麼select查詢的字段只能是post,想要獲取組內的其餘相關信息,須要藉助函數。

mysql> select post from employee GROUP BY post;
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| operation                               |
| sale                                    |
| teacher                                 |
| 老男孩駐沙河辦事處外交大使              |
+-----------------------------------------+
4 rows in set (0.01 sec)

mysql> select * from employee GROUP BY post;    
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db2.employee.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

(2)GROUP BY關鍵字和GROUP_CONCAT()函數一塊兒使用

  GROUP_CONCAT()函數將對組裏的對應記錄進行拼接

#按照崗位分組,並查看組內成員名
mysql> SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;
+-----------------------------------------+---------------------------------------------------------+
| post                                    | GROUP_CONCAT(name)                                      |
+-----------------------------------------+---------------------------------------------------------+
| operation                               | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |
| sale                                    | 歪歪,丫丫,丁丁,星星,格格                                |
| teacher                                 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |
| 老男孩駐沙河辦事處外交大使              | egon                                                    |
+-----------------------------------------+---------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> SELECT post,GROUP_CONCAT(name) as emp_members FROM employee GROUP BY post;
+-----------------------------------------+---------------------------------------------------------+
| post                                    | emp_members                                             |
+-----------------------------------------+---------------------------------------------------------+
| operation                               | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |
| sale                                    | 歪歪,丫丫,丁丁,星星,格格                                |
| teacher                                 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |
| 老男孩駐沙河辦事處外交大使              | egon                                                    |
+-----------------------------------------+---------------------------------------------------------+
4 rows in set (0.00 sec)

(3)GROUP BY與聚合函數一塊兒使用

mysql> select post,count(id) as count from employee group by post;   # 按照崗位分組,並查看每一個組有多少人
+-----------------------------------------+-------+
| post                                    | count |
+-----------------------------------------+-------+
| operation                               |     5 |
| sale                                    |     5 |
| teacher                                 |     7 |
| 老男孩駐沙河辦事處外交大使              |     1 |
+-----------------------------------------+-------+
4 rows in set (0.00 sec)

  強調:若是咱們用unique的字段做爲分組的依據,則每一條記錄自成一組,這種分組沒有意義。

     多條記錄之間的某個字段值相同,該字段一般用來做爲分組的依據。

     分組以後,只能取分組的字段,以及每一個組聚合函數的結果。

四、聚合函數

  注意:聚合函數聚合的是組的內容若沒有分組,則默認分爲一組,所以沒有分組也可使用聚合函數。經常使用聚合函數以下:

 SELECT COUNT(*) FROM employee;
 SELECT COUNT(*) FROM employee WHERE depart_id=1;
 SELECT MAX(salary) FROM employee;
 SELECT MIN(salary) FROM employee;
 SELECT AVG(salary) FROM employee;
 SELECT SUM(salary) FROM employee;
 SELECT SUM(salary) FROM employee WHERE depart_id=3;
# 利用聚合函數查看分組狀況
# 統計每一個部門員工人數
mysql> select count(id) from employee group by post;
+-----------+
| count(id) |
+-----------+
|         5 |
|         5 |
|         7 |
|         1 |
+-----------+
4 rows in set (0.00 sec)

# 統計公司總人數
mysql> select count(*) from employee;  
+----------+
| count(*) |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)

# 每一個職位有多少個員工?count()
mysql> select post, count(id) from employee group by post;
+-----------------------------------------+-----------+
| post                                    | count(id) |
+-----------------------------------------+-----------+
| operation                               |         5 |
| sale                                    |         5 |
| teacher                                 |         7 |
| 老男孩駐沙河辦事處外交大使              |         1 |
+-----------------------------------------+-----------+
4 rows in set (0.00 sec)

# 爲員工統計取別名 count()
mysql> select post,count(id) as emp_count from employee group by post;
+-----------------------------------------+-----------+
| post                                    | emp_count |
+-----------------------------------------+-----------+
| operation                               |         5 |
| sale                                    |         5 |
| teacher                                 |         7 |
| 老男孩駐沙河辦事處外交大使              |         1 |
+-----------------------------------------+-----------+
4 rows in set (0.00 sec)

# 取每一個部門的最大工資sum()
mysql> select post,max(salary) as max_salary from employee group by post;
+-----------------------------------------+------------+
| post                                    | max_salary |
+-----------------------------------------+------------+
| operation                               |   20000.00 |
| sale                                    |    4000.33 |
| teacher                                 | 1000000.31 |
| 老男孩駐沙河辦事處外交大使              |    7300.33 |
+-----------------------------------------+------------+
4 rows in set (0.00 sec)

# 取每一個部門最小工資,min()
mysql> select post,min(salary) as min_salary from employee group by post;
+-----------------------------------------+------------+
| post                                    | min_salary |
+-----------------------------------------+------------+
| operation                               |   10000.13 |
| sale                                    |    1000.37 |
| teacher                                 |    2100.00 |
| 老男孩駐沙河辦事處外交大使              |    7300.33 |
+-----------------------------------------+------------+
4 rows in set (0.00 sec)

# 取每一個部門的平均工資,avg()
mysql> select post,avg(salary) as avg_salary from employee group by post;
+-----------------------------------------+---------------+
| post                                    | avg_salary    |
+-----------------------------------------+---------------+
| operation                               |  16800.026000 |
| sale                                    |   2600.294000 |
| teacher                                 | 151842.901429 |
| 老男孩駐沙河辦事處外交大使              |   7300.330000 |
+-----------------------------------------+---------------+
4 rows in set (0.00 sec)

# 取每一個部門員工工資總和,sum()
mysql> select post,sum(salary) as sum_salary from employee group by post;
+-----------------------------------------+------------+
| post                                    | sum_salary |
+-----------------------------------------+------------+
| operation                               |   84000.13 |
| sale                                    |   13001.47 |
| teacher                                 | 1062900.31 |
| 老男孩駐沙河辦事處外交大使              |    7300.33 |
+-----------------------------------------+------------+
4 rows in set (0.00 sec)

# 取id爲3的部門員工年齡總和
mysql> select post,sum(age)  from employee where depart_id =3 group by post;
+-----------+----------+
| post      | sum(age) |
+-----------+----------+
| operation |      100 |
+-----------+----------+
1 row in set (0.00 sec)

mysql> select post,sum(age)  from employee where depart_id =3 group by post;
聚合函數操做實例

五、小練習

1. 查詢崗位名以及崗位包含的全部員工名字
2. 查詢崗位名以及各崗位內包含的員工個數
3. 查詢公司內男員工和女員工的個數
4. 查詢崗位名以及各崗位的平均薪資
5. 查詢崗位名以及各崗位的最高薪資
6. 查詢崗位名以及各崗位的最低薪資
7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
練習題
mysql> select post,GROUP_CONCAT(name) as dev_members from employee group by post;
+-----------------------------------------+---------------------------------------------------------+
| post                                    | dev_members                                             |
+-----------------------------------------+---------------------------------------------------------+
| operation                               | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |
| sale                                    | 歪歪,丫丫,丁丁,星星,格格                                |
| teacher                                 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |
| 老男孩駐沙河辦事處外交大使              | egon                                                    |
+-----------------------------------------+---------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> select post,count(id) from employee group by post;                                  
+-----------------------------------------+-----------+
| post                                    | count(id) |
+-----------------------------------------+-----------+
| operation                               |         5 |
| sale                                    |         5 |
| teacher                                 |         7 |
| 老男孩駐沙河辦事處外交大使              |         1 |
+-----------------------------------------+-----------+
4 rows in set (0.00 sec)

mysql> select sex,count(id) from employee group by sex;
+--------+-----------+
| sex    | count(id) |
+--------+-----------+
| male   |        10 |
| female |         8 |
+--------+-----------+
2 rows in set (0.00 sec)

mysql> select post,avg(salary) as avg_salary from employee group by post; 
+-----------------------------------------+---------------+
| post                                    | avg_salary    |
+-----------------------------------------+---------------+
| operation                               |  16800.026000 |
| sale                                    |   2600.294000 |
| teacher                                 | 151842.901429 |
| 老男孩駐沙河辦事處外交大使              |   7300.330000 |
+-----------------------------------------+---------------+
4 rows in set (0.00 sec)

mysql> select post,max(salary) as max_salary from employee group by post;      
+-----------------------------------------+------------+
| post                                    | max_salary |
+-----------------------------------------+------------+
| operation                               |   20000.00 |
| sale                                    |    4000.33 |
| teacher                                 | 1000000.31 |
| 老男孩駐沙河辦事處外交大使              |    7300.33 |
+-----------------------------------------+------------+
4 rows in set (0.00 sec)

mysql> select post,min(salary) as min_salary from employee group by post;      
+-----------------------------------------+------------+
| post                                    | min_salary |
+-----------------------------------------+------------+
| operation                               |   10000.13 |
| sale                                    |    1000.37 |
| teacher                                 |    2100.00 |
| 老男孩駐沙河辦事處外交大使              |    7300.33 |
+-----------------------------------------+------------+
4 rows in set (0.00 sec)

mysql> select sex,avg(salary) as avg_sex_salary from employee group by sex;        
+--------+----------------+
| sex    | avg_sex_salary |
+--------+----------------+
| male   |  110920.077000 |
| female |    7250.183750 |
+--------+----------------+
2 rows in set (0.00 sec)
練習題答案

6、HAVING過濾

HAVING與WHERE不同的地方?

  執行優先級從高到低:where > group by > having

  一、where發生在分組group by以前,所以where中能夠有任意字段,可是絕對不能使用聚合函數

  二、having發生分組group by以後,所以having中可使用分組的字段,沒法直接取到其餘字段,可使用聚合函數

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from employee where salary > 10000;
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary     | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
|  2 | alex      | male   |  78 | 2015-03-02 | teacher   | NULL         | 1000000.31 |    401 |         1 |
|  7 | jinxin    | male   |  18 | 1900-03-01 | teacher   | NULL         |   30000.00 |    401 |         1 |
| 14 | 張野      | male   |  28 | 2016-03-11 | operation | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金    | male   |  18 | 1997-03-12 | operation | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | operation | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | operation | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | operation | NULL         |   17000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
7 rows in set (0.00 sec)

mysql> select * from employee having salary > 10000;     
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary     | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
|  2 | alex      | male   |  78 | 2015-03-02 | teacher   | NULL         | 1000000.31 |    401 |         1 |
|  7 | jinxin    | male   |  18 | 1900-03-01 | teacher   | NULL         |   30000.00 |    401 |         1 |
| 14 | 張野      | male   |  28 | 2016-03-11 | operation | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金    | male   |  18 | 1997-03-12 | operation | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | operation | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | operation | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | operation | NULL         |   17000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
7 rows in set (0.00 sec)

mysql> select post,group_concat(name) from employee group by post having salary > 10000;  # 錯誤,分組後沒法直接取到salary字段
ERROR 1054 (42S22): Unknown column 'salary' in 'having clause'

# 沒法直接取到其餘字段,可使用聚合函數 mysql
> select post,group_concat(name) from employee group by post having avg(salary) > 10000; +-----------+---------------------------------------------------------+ | post | group_concat(name) | +-----------+---------------------------------------------------------+ | operation | 張野,程咬金,程咬銀,程咬銅,程咬鐵 | | teacher | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍 | +-----------+---------------------------------------------------------+ 2 rows in set (0.00 sec)

小練習:

1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
2. 查詢各崗位平均薪資大於10000的崗位名、平均工資
3. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資
# 1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
# 員工個數過濾前
mysql> select post, group_concat(name), count(id) from employee group by post;
+-----------------------------------------+---------------------------------------------------------+-----------+
| post                                    | group_concat(name)                                      | count(id) |
+-----------------------------------------+---------------------------------------------------------+-----------+
| operation                               | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |         5 |
| sale                                    | 歪歪,丫丫,丁丁,星星,格格                                |         5 |
| teacher                                 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |         7 |
| 老男孩駐沙河辦事處外交大使              | egon                                                    |         1 |
+-----------------------------------------+---------------------------------------------------------+-----------+
4 rows in set (0.00 sec)

# 員工個數過濾後
mysql> select post, group_concat(name), count(id) from employee group by post having count(id) < 2;
+-----------------------------------------+--------------------+-----------+
| post                                    | group_concat(name) | count(id) |
+-----------------------------------------+--------------------+-----------+
| 老男孩駐沙河辦事處外交大使              | egon               |         1 |
+-----------------------------------------+--------------------+-----------+
1 row in set (0.00 sec)

# 2.查詢各崗位平均薪資大於10000的崗位名、平均工資
mysql> select post, avg(salary) as avg_salary from employee group by post having avg_salary > 10000; 
+-----------+---------------+
| post      | avg_salary    |
+-----------+---------------+
| operation |  16800.026000 |
| teacher   | 151842.901429 |
+-----------+---------------+
2 rows in set (0.00 sec)

# 3.查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資
mysql> select post, avg(salary) as avg_salary from employee group by post having avg_salary between 10000 and 20000;
+-----------+--------------+
| post      | avg_salary   |
+-----------+--------------+
| operation | 16800.026000 |
+-----------+--------------+
1 row in set (0.00 sec)
練習題答案

7、查詢排序:ORDER BY

 按單列排序:

  ORDER BY...: 默認是升序排列

  ORDER BY ... ASC:升序排列

  ORDER BY ... DESC:降序排列

mysql> select * from employee ORDER BY salary;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

mysql> select * from employee ORDER BY salary ASC;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

mysql> select * from employee ORDER BY salary DESC;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)
單列排序示例

按多列排序:

  上面是按工資排,若是按age排序,有不少人年紀是相同的,這時就須要採用多列排序了。

  例如:先按照age升序排,若是age相同按照id降序排

mysql> select * from employee
    -> ORDER BY AGE,
    -> id DESC;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    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.00 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

  有order by的sql語句,執行順序(注意與語法順序的異同):

  先執行from找到表;再按照where約束條件過濾數據;再交給group by進行分組;接着交給having進行過濾;

過濾以後運行distinct進行去重(沒有distinct就不去重);去重以後再執行order by進行排序;orderby 運行完以後運行limit。

  最須要注意的一點:where這一步的時候尚未分組,所以where不能用聚合函數。體會下面這個用例

mysql> select * from employee where max(salary) > 1000;
ERROR 1111 (HY000): Invalid use of group function
mysql> select max(salary) from employee where salary > 1000;
+-------------+
| max(salary) |
+-------------+
|  1000000.31 |
+-------------+
1 row in set (0.00 sec)

  另外一個關注的重點:distinct語句執行是在having後面。體會下面這個用例:(用例實際執行和教材不一樣,需關注)

  having不能使用distinct定義的別名,order by 則可使用。

mysql> select post, count(id) as emp_count from employee
    -> where salary > 1000
    -> group by post
    -> having emp_count > 5;   # 此處引用不了emp_count別名,由於distinct尚未執行
+---------+-----------+
| post    | emp_count |
+---------+-----------+
| teacher |         7 |
+---------+-----------+
1 row in set (0.00 sec)

mysql> select post, count(id) as emp_count from employee where salary > 1000 group by post having count(id) > 5;         
+---------+-----------+
| post    | emp_count |
+---------+-----------+
| teacher |         7 |
+---------+-----------+
1 row in set (0.00 sec)

小練習:

1. 查詢全部員工信息,先按照age升序排序,若是age相同則按照hire_date降序排序
2. 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列
3. 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列
# 1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
mysql> select * from employee ORDER BY age ASC, hire_date DESC;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 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 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    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.00 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

# 分組以後,distinct這裏只能查被分組的字段和聚合函數
# 2.查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列
mysql> select post, avg(salary),group_concat(name) from employee GROUP BY post having avg(salary) > 10000 order by avg(salary) ASC;
+-----------+---------------+---------------------------------------------------------+
| post      | avg(salary)   | group_concat(name)                                      |
+-----------+---------------+---------------------------------------------------------+
| operation |  16800.026000 | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |
| teacher   | 151842.901429 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |
+-----------+---------------+---------------------------------------------------------+
2 rows in set (0.00 sec)

# 3. 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列
mysql> select post, avg(salary),group_concat(name) from employee GROUP BY post having avg(salary) > 10000 order by avg(salary) DESC;
+-----------+---------------+---------------------------------------------------------+
| post      | avg(salary)   | group_concat(name)                                      |
+-----------+---------------+---------------------------------------------------------+
| teacher   | 151842.901429 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龍   |
| operation |  16800.026000 | 張野,程咬金,程咬銀,程咬銅,程咬鐵                        |
+-----------+---------------+---------------------------------------------------------+
2 rows in set (0.00 sec)
練習題答案

8、限制查詢的記錄數:LIMIT

  LIMIT語法上是寫在最後,執行時也是最後一個執行。

  限制最終打印在屏幕上的條數。

mysql> select * from employee order by salary desc
    -> limit 3;
+----+-----------+------+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name      | sex  | age | hire_date  | post      | post_comment | salary     | office | depart_id |
+----+-----------+------+-----+------------+-----------+--------------+------------+--------+-----------+
|  2 | alex      | male |  78 | 2015-03-02 | teacher   | NULL         | 1000000.31 |    401 |         1 |
|  7 | jinxin    | male |  18 | 1900-03-01 | teacher   | NULL         |   30000.00 |    401 |         1 |
| 15 | 程咬金    | male |  18 | 1997-03-12 | operation | NULL         |   20000.00 |    403 |         3 |
+----+-----------+------+-----+------------+-----------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)

# 找出工資最高人的詳細信息
mysql> select * from employee order by salary desc limit 1;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec)

  LIMIT還可以實現分頁功能(可是使用LIMIT來分頁效率不高,通常仍是須要一些緩存機制來幫忙實現分頁)

# limit作分頁功能
mysql> select * from employee order by salary DESC
    -> limit 0,5;
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary     | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
|  2 | alex      | male   |  78 | 2015-03-02 | teacher   | NULL         | 1000000.31 |    401 |         1 |
|  7 | jinxin    | male   |  18 | 1900-03-01 | teacher   | NULL         |   30000.00 |    401 |         1 |
| 15 | 程咬金    | male   |  18 | 1997-03-12 | operation | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | operation | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | operation | NULL         |   18000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+------------+--------+-----------+
5 rows in set (0.01 sec)

mysql> select * from employee order by salary desc
    -> limit 5,5;   # 從第5開始,即先查詢出第6條,而後包含這一條在內日後查5條
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation | NULL         | 17000.00 |    403 |         3 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher   | NULL         | 10000.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher   | NULL         |  9000.00 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher   | NULL         |  8300.00 |    401 |         1 |
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec)
limit來實現分頁顯示

9、正則查詢

   更增強大的模糊匹配——正則表達式。

   調用正則表達式查詢的關鍵字——REGEXP

# 名字是'jin'開頭的記錄
mysql> select * from employee where name regexp '^jin';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
2 rows in set (0.01 sec)

# 名字是'jin'開頭,且以g或n結尾的記錄
mysql> select * from employee where name regexp '^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.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
2 rows in set (0.00 sec)

# 名字以on結尾的記錄
mysql> select * from employee where name regexp 'on$';
+----+------+------+-----+------------+-----------------------------------------+--------------+---------+--------+-----------+
| 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 |
+----+------+------+-----+

# 匹配m字符兩次'mm'
mysql> select * from employee where name regexp 'm{2}'; 
Empty set (0.00 sec)

mysql> select * from employee where name regexp 'g{1}';
+----+------------+--------+-----+------------+-----------------------------------------+--------------+---------+--------+-----------+
| 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 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         | 9000.00 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+---------+--------+-----------+
2 rows in set (0.00 sec)
正則表達式使用示例
小結:對字符串匹配的方式
WHERE name = 'egon';
WHERE name LIKE 'yua%';
WHERE name REGEXP 'on$';

10、單表查詢的語法順序和執行順序總結

語法順序:

select distinct 字段1,字段2,字段3 from 庫.表
    where 條件
    group by 分組條件
    having  過濾
    order by  排序字段
    limit n;

執行順序:

  select執行了一個打印,各個關鍵字關係用僞代碼表示以下:

def from(db, table):
    f = open(r'%s\%s' %(db, table))
    return f

def where(condition, f):
    for line in f:
        if condition:
            yield line

def group(lines):
    pass

def having(group_res):
    pass

def distinct(having_res):
    pass

def order(distinct_res):
    pass

def limit(order_res):
    pass

def select():
    from('db1', 't1')
    lines=where('id>3',f)
    group_res=group(lines)
    having_res=having(group_res)
    distinct_res=distinct(having_res)
    order_res=order(distinct_res)
    res=limit(order_res)
    print(res)
    return res
執行順序僞代碼
相關文章
相關標籤/搜索