七 .數據庫(表的查詢)

一 .表的查詢

1. 表單查詢及關鍵字和查詢優先級

語法:
SELECT DISTINCT 字段1,字段2... FROM 表名 WHERE 條件 GROUP BY field HAVING 篩選 ORDER BY field LIMIT 限制條數
優先級

from
:找到表 where:拿着where指定的約束條件,去文件/表中取出一條條記錄 group by:將取出的一條條記錄進行分組group by,若是沒有group by,則總體做爲一組 select:執行select distinct:去重 having:將分組的結果進行having過濾 order by:將結果按條件排序:order by limit:限制結果的顯示條數
mysql> select * from cc;
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| id | name      | sex | age | hire_date  | post                           | post_comment | salary     | office | depart_id |
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| 19 | nick      | 男  |  18 | 2017-03-01 | 老男孩駐上海虹橋最帥           | NULL         |    7300.33 |    401 |         1 |
| 20 | jason     | 男  |  78 | 2015-03-02 | teacher                        | NULL         | 1000000.31 |    401 |         1 |
| 21 | sean      | 女  |  81 | 2013-03-05 | teacher                        | NULL         |    8300.00 |    401 |         1 |
| 22 | tank      | 男  |  73 | 2014-07-01 | teacher                        | NULL         |    3500.00 |    401 |         1 |
| 23 | oscar     | 女  |  28 | 2012-11-01 | teacher                        | NULL         |    2100.00 |    401 |         1 |
| 24 | mac       | 男  |  18 | 2011-02-11 | teacher                        | NULL         |    9000.00 |    401 |         1 |
| 25 | rocky     | 女  |  18 | 1900-03-01 | teacher                        | NULL         |   30000.00 |    401 |         1 |
| 26 | 成龍      | 男  |  48 | 2010-11-11 | teacher                        | NULL         |   10000.00 |    401 |         1 |
| 27 | 歪歪      | 女  |  48 | 2015-03-11 | sale                           | NULL         |    3000.13 |    402 |         2 |
| 28 | 丫丫      | 男  |  38 | 2010-11-01 | sale                           | NULL         |    2000.35 |    402 |         2 |
| 29 | 丁丁      | 女  |  18 | 2011-03-12 | sale                           | NULL         |    1000.37 |    402 |         2 |
| 30 | 星星      | 男  |  18 | 2016-05-13 | sale                           | NULL         |    3000.29 |    402 |         2 |
| 31 | 格格      | 男  |  28 | 2017-01-27 | sale                           | NULL         |    4000.33 |    402 |         2 |
| 32 | 張野      | 男  |  28 | 2016-03-11 | operation                      | NULL         |   10000.13 |    403 |         3 |
| 33 | 程咬金    | 女  |  18 | 1997-03-12 | operation                      | NULL         |   20000.00 |    403 |         3 |
| 34 | 程咬銀    | 男  |  18 | 2013-03-11 | operation                      | NULL         |   19000.00 |    403 |         3 |
| 35 | 程咬銅    | 男  |  18 | 2015-04-11 | operation                      | NULL         |   18000.00 |    403 |         3 |
| 36 | 程咬鐵    | 女  |  18 | 2014-05-12 | operation                      | NULL         |   17000.00 |    403 |         3 |
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+

1.簡單查詢

mysql> select sex,age from cc;
+-----+-----+
| sex | age |
+-----+-----+
| 男 | 18 |
| 男 | 78 |
| 女 | 81 |
| 男 | 73 |
| 女 | 28 |
| 男 | 18 |
| 女 | 18 |
| 男 | 48 |
| 女 | 48 |
| 男 | 38 |
| 女 | 18 |
| 男 | 18 |
| 男 | 28 |
| 男 | 28 |
| 女 | 18 |
| 男 | 18 |
| 男 | 18 |
| 女 | 18 |
+-----+-----+mysql

mysql> select distinct age from cc;      distinct避免重複
+-----+
| age |
+-----+
| 18 |
| 78 |
| 81 |
| 73 |
| 28 |
| 48 |
| 38 |
+-----+sql

mysql> select distinct post from cc;      查出全部的崗位(去掉重複 disinct)函數

+--------------------------------+
| post |
+--------------------------------+
| 老男孩駐上海虹橋最帥 |
| teacher |
| sale |
| operation |
+--------------------------------+
4 rows in set (0.00 sec)post

 

2.經過四則運算查詢

mysql> select name,salary*2 from cc; 
+-----------+------------+
| name | salary*2 |
+-----------+------------+
| nick | 14600.66 |
| jason | 2000000.62 |
| sean | 16600.00 |
| tank | 7000.00 |
| oscar | 4200.00 |
| mac | 18000.00 |
| rocky | 60000.00 |
| 成龍 | 20000.00 |
| 歪歪 | 6000.26 |
| 丫丫 | 4000.70 |
| 丁丁 | 2000.74 |
| 星星 | 6000.58 |
| 格格 | 8000.66 |
| 張野 | 20000.26 |
| 程咬金 | 40000.00 |
| 程咬銀 | 38000.00 |
| 程咬銅 | 36000.00 |
| 程咬鐵 | 34000.00 |
+-----------+------------+spa

mysql> select name,salary*2 as bb from cc;
+-----------+------------+
| name | bb |
+-----------+------------+
| nick | 14600.66 |
| jason | 2000000.62 |
| sean | 16600.00 |
| tank | 7000.00 |
| oscar | 4200.00 |
| mac | 18000.00 |
| rocky | 60000.00 |
| 成龍 | 20000.00 |
| 歪歪 | 6000.26 |
| 丫丫 | 4000.70 |
| 丁丁 | 2000.74 |
| 星星 | 6000.58 |
| 格格 | 8000.66 |
| 張野 | 20000.26 |
| 程咬金 | 40000.00 |
| 程咬銀 | 38000.00 |
| 程咬銅 | 36000.00 |
| 程咬鐵 | 34000.00 |
+-----------+------------+
18 rows in set (0.00 seccode

 
 

mysql> select name ,salary*2 bb from cc;
+-----------+------------+
| name | bb |
+-----------+------------+
| nick | 14600.66 |
| jason | 2000000.62 |
| sean | 16600.00 |
| tank | 7000.00 |
| oscar | 4200.00 |
| mac | 18000.00 |
| rocky | 60000.00 |
| 成龍 | 20000.00 |
| 歪歪 | 6000.26 |
| 丫丫 | 4000.70 |
| 丁丁 | 2000.74 |
| 星星 | 6000.58 |
| 格格 | 8000.66 |
| 張野 | 20000.26 |
| 程咬金 | 40000.00 |
| 程咬銀 | 38000.00 |
| 程咬銅 | 36000.00 |
| 程咬鐵 | 34000.00 |
+-----------+------------+blog

mysql> select name ,salary*2 bb from cc;  查出全部員工名字,以及他們的年薪,年薪的字段名爲bb排序

+-----------+------------+
| name | bb |
+-----------+------------+
| nick | 14600.66 |
| jason | 2000000.62 |
| sean | 16600.00 |
| tank | 7000.00 |
| oscar | 4200.00 |
| mac | 18000.00 |
| rocky | 60000.00 |
| 成龍 | 20000.00 |
| 歪歪 | 6000.26 |
| 丫丫 | 4000.70 |
| 丁丁 | 2000.74 |
| 星星 | 6000.58 |
| 格格 | 8000.66 |
| 張野 | 20000.26 |
| 程咬金 | 40000.00 |
| 程咬銀 | 38000.00 |
| 程咬銅 | 36000.00 |
| 程咬鐵 | 34000.00 |
+-----------+------------+

字符串

3.定義顯示格式查詢string

mysql> select concat("姓名"'name',"年薪","salary") as vv from cc;     CONCAT() 函數用於鏈接字符串
+------------------------+
| vv |
+------------------------+
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
| 年薪姓名namesalary |
+------------------------+

mysql> select concat('<名字:',name,'> ','<薪資:',salary,'>') from cc;  查出全部員工的名字,薪資,格式爲<名字:nick> <薪資:3000>

+-------------------------------------------------------+
| concat('<名字:',name,'> ','<薪資:',salary,'>') |
+-------------------------------------------------------+
| <名字:nick> <薪資:7300.33> |
| <名字:jason> <薪資:1000000.31> |
| <名字:sean> <薪資:8300.00> |
| <名字:tank> <薪資:3500.00> |
| <名字:oscar> <薪資:2100.00> |
| <名字:mac> <薪資:9000.00> |
| <名字:rocky> <薪資:30000.00> |
| <名字:成龍> <薪資:10000.00> |
| <名字:歪歪> <薪資:3000.13> |
| <名字:丫丫> <薪資:2000.35> |
| <名字:丁丁> <薪資:1000.37> |
| <名字:星星> <薪資:3000.29> |
| <名字:格格> <薪資:4000.33> |
| <名字:張野> <薪資:10000.13> |
| <名字:程咬金> <薪資:20000.00> |
| <名字:程咬銀> <薪資:19000.00> |
| <名字:程咬銅> <薪資:18000.00> |
| <名字:程咬鐵> <薪資:17000.00> |
+-------------------------------------------------------+

 

2.條件約束查詢(where)

比較運算符:> < >= <= <> != between 80 and 100 值在80到100之間 in(80,90,100) 值是80或90或100 like 'n%' 通配符能夠是%或_, %表示任意多字符 _表示一個字符 邏輯運算符:在多個條件直接可使用邏輯運算符 and or not
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| id | name      | sex | age | hire_date  | post                           | post_comment | salary     | office | depart_id |
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| 19 | nick      | 男  |  18 | 2017-03-01 | 老男孩駐上海虹橋最帥           | NULL         |    7300.33 |    401 |         1 |
| 20 | jason     | 男  |  78 | 2015-03-02 | teacher                        | NULL         | 1000000.31 |    401 |         1 |
| 21 | sean      | 女  |  81 | 2013-03-05 | teacher                        | NULL         |    8300.00 |    401 |         1 |
| 22 | tank      | 男  |  73 | 2014-07-01 | teacher                        | NULL         |    3500.00 |    401 |         1 |
| 23 | oscar     | 女  |  28 | 2012-11-01 | teacher                        | NULL         |    2100.00 |    401 |         1 |
| 24 | mac       | 男  |  18 | 2011-02-11 | teacher                        | NULL         |    9000.00 |    401 |         1 |
| 25 | rocky     | 女  |  18 | 1900-03-01 | teacher                        | NULL         |   30000.00 |    401 |         1 |
| 26 | 成龍      | 男  |  48 | 2010-11-11 | teacher                        | NULL         |   10000.00 |    401 |         1 |
| 27 | 歪歪      | 女  |  48 | 2015-03-11 | sale                           | NULL         |    3000.13 |    402 |         2 |
| 28 | 丫丫      | 男  |  38 | 2010-11-01 | sale                           | NULL         |    2000.35 |    402 |         2 |
| 29 | 丁丁      | 女  |  18 | 2011-03-12 | sale                           | NULL         |    1000.37 |    402 |         2 |
| 30 | 星星      | 男  |  18 | 2016-05-13 | sale                           | NULL         |    3000.29 |    402 |         2 |
| 31 | 格格      | 男  |  28 | 2017-01-27 | sale                           | NULL         |    4000.33 |    402 |         2 |
| 32 | 張野      | 男  |  28 | 2016-03-11 | operation                      | NULL         |   10000.13 |    403 |         3 |
| 33 | 程咬金    | 女  |  18 | 1997-03-12 | operation                      | NULL         |   20000.00 |    403 |         3 |
| 34 | 程咬銀    | 男  |  18 | 2013-03-11 | operation                      | NULL         |   19000.00 |    403 |         3 |
| 35 | 程咬銅    | 男  |  18 | 2015-04-11 | operation                      | NULL         |   18000.00 |    403 |         3 |
| 36 | 程咬鐵    | 女  |  18 | 2014-05-12 | operation                      | NULL         |   17000.00 |    403 |         3 |
+----+-----------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
1.單條件查詢

mysql> select name from cc where post='sale'; 查詢工做對應的老師名 +--------+ | name | +--------+ | 歪歪 | | 丫丫 | | 丁丁 | | 星星 | | 格格 | +--------+


2.多條件查詢

mysql> select name,salary from cc where post="teacher" and salary>10000;  查看薪水大於10000 而且職業爲老師 對應的名字 和薪水
+-------+------------+
| name | salary |
+-------+------------+
| jason | 1000000.31 |
| rocky | 30000.00 |
+-------+------------+

3.關鍵字查詢(between  and)

mysql> select name,salary from cc where salary between 10000 and 20000;  查看薪水在10000----20000之間 對應的名字
+-----------+----------+
| name | salary |
+-----------+----------+
| 成龍 | 10000.00 |
| 張野 | 10000.13 |
| 程咬金 | 20000.00 |
| 程咬銀 | 19000.00 |
| 程咬銅 | 18000.00 |
| 程咬鐵 | 17000.00 |
+-----------+----------+
6 rows in set (0.28 sec)

4. 關鍵字is null(判斷某個字段是否爲null不能用等號,須要用is)

mysql> select name ,post_comment from cc where post_comment is null;   查看post_comment爲空對應的名字
+-----------+--------------+
| name | post_comment |
+-----------+--------------+
| nick | NULL |
| jason | NULL |
| sean | NULL |
| tank | NULL |
| oscar | NULL |
| mac | NULL |
| rocky | NULL |
| 成龍 | NULL |
| 歪歪 | NULL |
| 丫丫 | NULL |
| 丁丁 | NULL |
| 星星 | NULL |
| 格格 | NULL |
| 張野 | NULL |
| 程咬金 | NULL |
| 程咬銀 | NULL |
| 程咬銅 | NULL |
| 程咬鐵 | NULL |
+-----------+--------------+
18 rows in set (0.00 sec)

5.關鍵字in集合查詢

mysql> select name,salary from cc where salary=3000 or salary=3500;
+------+---------+
| name | salary |
+------+---------+
| tank | 3500.00 |
+------+---------+
1 row in set (0.00 sec)

mysql> select name,salary from cc where salary in (3000,4000,9000,12000);   查看in集合裏面又沒符合表裏面的薪水 所對應的老師
+------+---------+
| name | salary |
+------+---------+
| mac | 9000.00 |
+------+---------+
1 row in set (0.29 sec)

 6. 關鍵字like模糊查詢 % __

mysql> select * from cc where name like 'ni%';      查看名字中含有ni對應的老師
+----+------+-----+-----+------------+--------------------------------+--------------+---------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------+-----+-----+------------+--------------------------------+--------------+---------+--------+-----------+
| 19 | nick | 男 | 18 | 2017-03-01 | 老男孩駐上海虹橋最帥 | NULL | 7300.33 | 401 | 1 |
+----+------+-----+-----+------------+--------------------------------+--------------+---------+--------+-----------+
1 row in set (0.30 sec)


通配符’_’ SELECT * FROM employee WHERE emp_name LIKE 'ja__';

通配符’%’ SELECT * FROM employee WHERE emp_name LIKE 'ni%';

7. 練習
select emp_name,age from employee where post = 'teacher'; 查看崗位是teacher的員工姓名、年齡
select emp_name,age
from employee where post='teacher' and age > 30; 查看崗位是teacher且年齡大於30歲的員工姓名、年齡
select emp_name,age,salary
from employee where post='teacher' and salary between 9000 and 10000; 查看崗位是teacher且薪資在9000-1000範員工姓名、年齡、薪資
select
* from employee where post_comment is not null;查看崗位描述不爲NULL的員工信息
select emp_name,age,salary
from employee where post='teacher' and salary in (10000,9000,30000);查看崗位是teacher且薪資是10000或9000或30000的員工姓名、年齡、薪資
select emp_name,age,salary
from employee where post='teacher' and salary not in (10000,9000,30000);查看崗位是teacher且薪資不是10000或9000或30000的員工姓名、年齡、薪資
select emp_name,salary
*12 from employee where post='teacher' and emp_name like 'mac%';查看崗位是teacher且名字是jin開頭的員工姓名、年薪

 

3. 分組查詢(group by)

1.單獨使用GROUP BY關鍵字分組
mysql> select post from cc group by post;   意思把因此職業分爲一組 +--------------------------------+
| post                           |
+--------------------------------+
| 老男孩駐上海虹橋最帥           |
| teacher                        |
| sale                           |
| operation                      |
+--------------------------------+
4 rows in set (0.00 sec)

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

select post,group_concat(name) from cc group by post;   並按照組分 查看裏面全部成員
+--------------------------------+------------------------------------------------+
| post | group_concat(name) |
+--------------------------------+------------------------------------------------+
| operation | 張野,程咬金,程咬銀,程咬銅,程咬鐵 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | jason,sean,tank,oscar,mac,rocky,成龍 |
| 老男孩駐上海虹橋最帥 | nick |
+--------------------------------+------------------------------------------------+
4 rows in set (0.31 sec)

 

mysql> select post,group_concat(name) as lover from cc group by post;   # 按照崗位分組,並查看組內成員名
+--------------------------------+------------------------------------------------+
| post | lover |
+--------------------------------+------------------------------------------------+
| operation | 張野,程咬金,程咬銀,程咬銅,程咬鐵 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | jason,sean,tank,oscar,mac,rocky,成龍 |
| 老男孩駐上海虹橋最帥 | nick |
+--------------------------------+------------------------------------------------+
4 rows in set (0.00 sec)

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

mysql> select post,count(id) as count from cc group by post;# 按照崗位分組,並查看每一個組有多少人
+--------------------------------+-------+
| post | count |
+--------------------------------+-------+
| 老男孩駐上海虹橋最帥 | 1 |
| teacher | 7 |
| sale | 5 |
| operation | 5 |
+--------------------------------+-------+
4 rows in set (0.28 sec)

 
   
注意:若是咱們用unique的字段做爲分組的依據,則每一條記錄自成一組,這種分組沒有意義;多條記錄之間的某個字段值相同,該字段一般用來做爲分組的依據
 
  
 
  

 5.聚合函數

   強調:聚合函數聚合的是組的內容,如果沒有分組,則默認一組

    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;
1.練習聚合函數

mysql> select post,group_concat(name) from cc group by post;  查詢崗位名以及崗位包含的全部員工名字

+--------------------------------+------------------------------------------------+
| post | group_concat(name) |
+--------------------------------+------------------------------------------------+
| operation | 張野,程咬金,程咬銀,程咬銅,程咬鐵 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | jason,sean,tank,oscar,mac,rocky,成龍 |
| 老男孩駐上海虹橋最帥 | nick |
+--------------------------------+------------------------------------------------+
4 rows in set (0.00 sec)

mysql> select post,count(id) from cc group by post;查詢崗位名以及各崗位內包含的員工個數

+--------------------------------+-----------+
| post | count(id) |
+--------------------------------+-----------+
| 老男孩駐上海虹橋最帥 | 1 |
| teacher | 7 |
| sale | 5 |
| operation | 5 |
+--------------------------------+-----------+
4 rows in set (0.00 sec)

 

mysql> select sex,count(id) from cc group by sex;查詢公司內男員工和女員工的個數

+-----+-----------+
| sex | count(id) |
+-----+-----------+
| 男 | 11 |
| 女 | 7 |
+-----+-----------+
2 rows in set (0.00 sec)

 

mysql> select post,avg(salary) from cc group by post; 查詢崗位名以及各崗位的平均薪資  avg平均

+--------------------------------+---------------+
| post | avg(salary) |
+--------------------------------+---------------+
| 老男孩駐上海虹橋最帥 | 7300.330000 |
| teacher | 151842.901429 |
| sale | 2600.294000 |
| operation | 16800.026000 |
+--------------------------------+---------------+
4 rows in set (0.00 sec)

 

mysql> select post,max(salary) from cc group by post;查詢崗位名以及各崗位的最高薪資

+--------------------------------+-------------+
| post | max(salary) |
+--------------------------------+-------------+
| 老男孩駐上海虹橋最帥 | 7300.33 |
| teacher | 1000000.31 |
| sale | 4000.33 |
| operation | 20000.00 |
+--------------------------------+-------------+
4 rows in set (0.00 sec)

 

mysql> select post,min(salary) from cc group by post;查詢崗位名以及各崗位的最低薪資

+--------------------------------+-------------+
| post | min(salary) |
+--------------------------------+-------------+
| 老男孩駐上海虹橋最帥 | 7300.33 |
| teacher | 2100.00 |
| sale | 1000.37 |
| operation | 10000.13 |
+--------------------------------+-------------+
4 rows in set (0.00 sec)

 

mysql> select sex,avg(salary) from cc group by sex; 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資

+-----+--------------+
| sex | avg(salary) |
+-----+--------------+
| 男 | 98709.249091 |
| 女 | 11628.642857 |
+-----+--------------+

6.過濾(where having)

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

Where 發生在分組group by以前,於是Where中能夠有任意字段,可是絕對不能使用聚合函數。
Having發生在分組group by以後,於是Having中可使用分組的字段,沒法直接取到其餘字段,可使用聚合函數

1.簡單過濾查詢

mysql> select * from cc where salary>10000;   查詢薪水大於10000的
+----+-----------+-----+-----+------------+-----------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+-----+-----+------------+-----------+--------------+------------+--------+-----------+
| 20 | jason | 男 | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 25 | rocky | 女 | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 32 | 張野 | 男 | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 33 | 程咬金 | 女 | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 34 | 程咬銀 | 男 | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 35 | 程咬銅 | 男 | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 36 | 程咬鐵 | 女 | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+-----------+-----+-----+------------+-----------+--------------+------------+--------+-----------+
7 rows in set (0.00 sec)

 

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

mysql> select * from emp where salary > 100000;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | emp_name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | jason | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
row in set (0.00 sec)

mysql> select post,group_concat(emp_name) from emp group by post having salary > 10000;#錯誤,分組後沒法直接取到salary字段
ERROR 1054 (42S22): Unknown column 'salary' in 'having clause'
mysql> select post,group_concat(emp_name) from emp group by post having avg(salary) > 10000;
+-----------+-------------------------------------------------------+
| post | group_concat(emp_name) |
+-----------+-------------------------------------------------------+
| operation | 程咬鐵,程咬銅,程咬銀,程咬金,張野 |
| teacher | 成龍,rocky,mac,oscar,tank,sean,jason |
+-----------+-------------------------------------------------------+
rows in set (0.00 sec)
查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
查詢各崗位平均薪資大於10000的崗位名、平均工資
查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資
題目1
mysql> select post,group_concat(emp_name),count(id) from employee group by post having count(id) < 2;
+-----------------------------------------+--------------------+-----------+
| post                                    | group_concat(emp_name) | count(id) |
+-----------------------------------------+--------------------+-----------+
| 老男孩駐上海虹橋最帥              | nick               |         1 |
+-----------------------------------------+--------------------+-----------+

題目2
mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000;
+-----------+---------------+
| post      | avg(salary)   |
+-----------+---------------+
| operation |  16800.026000 |
| teacher   | 151842.901429 |
+-----------+---------------+

題目3
mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;
+-----------+--------------+
| post      | avg(salary)  |
+-----------+--------------+
| operation | 16800.026000 |

 7. 排序查詢(order by)

按單列排序
    SELECT * FROM employee ORDER BY salary; 默認是升序
    SELECT * FROM employee ORDER BY salary ASC; 升序
    SELECT * FROM employee ORDER BY salary DESC; 降序

按多列排序:先按照age排序,若是年紀相同,則按照薪資排序
    SELECT * from employee
        ORDER BY age,
        salary DESC;


查詢全部員工信息,先按照age升序排序,若是age相同則按照hire_date降序排序
查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列
查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列
題目1
mysql> select * from employee ORDER BY age asc,hire_date desc;

題目2
mysql> select post,avg(salary) from cc group by post having avg(salary) > 10000 order by avg(salary) asc;
+-----------+---------------+
| post      | avg(salary)   |
+-----------+---------------+
| operation |  16800.026000 |
| teacher   | 151842.901429 |
+-----------+---------------+

題目3
mysql> select post,avg(salary) from cc group by post having avg(salary) > 10000 order by avg(salary) desc;
+-----------+---------------+
| post      | avg(salary)   |
+-----------+---------------+
| teacher   | 151842.901429 |
| operation |  16800.026000 |
+-----------+---------------+
 

8.限制條數查詢(limit)

SELECT * FROM employee ORDER BY salary DESC 
    LIMIT 3;                    #默認初始位置爲0 

SELECT * FROM employee ORDER BY salary DESC
    LIMIT 0,5; #從第0開始,即先出第一條,而後包含這一條在內日後查5條

SELECT * FROM employee ORDER BY salary DESC
    LIMIT 5,5; #從第5開始,即先出第6條,而後包含這一條在內日後查5條
mysql> select * from cc limit 3; 查看3條數據 +----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| id | name  | sex | age | hire_date  | post                           | post_comment | salary     | office | depart_id |
+----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| 19 | nick  | 男  |  18 | 2017-03-01 | 老男孩駐上海虹橋最帥           | NULL         |    7300.33 |    401 |         1 |
| 20 | jason | 男  |  78 | 2015-03-02 | teacher                        | NULL         | 1000000.31 |    401 |         1 |
| 21 | sean  | 女  |  81 | 2013-03-05 | teacher                        | NULL         |    8300.00 |    401 |         1 |
+----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)
mysql> select * from cc limit 0,5; 查看0到五條數據 +----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| id | name  | sex | age | hire_date  | post                           | post_comment | salary     | office | depart_id |
+----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
| 19 | nick  | 男  |  18 | 2017-03-01 | 老男孩駐上海虹橋最帥           | NULL         |    7300.33 |    401 |         1 |
| 20 | jason | 男  |  78 | 2015-03-02 | teacher                        | NULL         | 1000000.31 |    401 |         1 |
| 21 | sean  | 女  |  81 | 2013-03-05 | teacher                        | NULL         |    8300.00 |    401 |         1 |
| 22 | tank  | 男  |  73 | 2014-07-01 | teacher                        | NULL         |    3500.00 |    401 |         1 |
| 23 | oscar | 女  |  28 | 2012-11-01 | teacher                        | NULL         |    2100.00 |    401 |         1 |
+----+-------+-----+-----+------------+--------------------------------+--------------+------------+--------+-----------+
5 rows in set (0.00 sec
mysql> select * from employee limit 5,5; 從第五個位置顯示五條數據 +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | emp_name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | mac | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | rocky     | 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 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.00 sec)
mysql> select * from employee limit 10,5; 從第10條數 據顯示5條數據 +----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | emp_name      | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 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 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
rows in set (0.00 sec)

9. 正則查詢

SELECT * FROM employee WHERE emp_name REGEXP '^jas';

SELECT * FROM employee WHERE emp_name REGEXP 'on$';

SELECT * FROM employee WHERE emp_name REGEXP 'm{2}';


小結:對字符串匹配的方式
WHERE emp_name = 'nick';
WHERE emp_name LIKE 'sea%';
WHERE emp_name REGEXP 'on$';
相關文章
相關標籤/搜索