1. 程式人生 > 資料庫 >MySQL單表查詢操作例項詳解【語法、約束、分組、聚合、過濾、排序等】

MySQL單表查詢操作例項詳解【語法、約束、分組、聚合、過濾、排序等】

本文例項講述了MySQL單表查詢操作。分享給大家供大家參考,具體如下:

語法

一、單表查詢的語法

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

二、關鍵字的執行優先順序(重點)

重點中的重點:關鍵字的執行優先順序

from
where
group by
having
select
distinct
order by
limit

1.找到表:from

2.拿著where指定的約束條件,去檔案/表中取出一條條記錄

3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作為一組

4.將分組的結果進行having過濾

5.執行select

6.去重

7.將結果按條件排序:order by

8.限制結果的顯示條數

(1)where 約束 

where運算子

where子句中可以使用
1.比較運算子:>、<、>=、<=、<>、!=
2.between 80 and 100 :值在80到100之間
3.in(80,90,100)值是10或20或30
4.like 'xiaomagepattern': pattern可以是%或者_。%小時任意多字元,_表示一個字元
5.邏輯運算子:在多個條件直接可以使用邏輯運算子 and or not

(2)group by 分組查詢

#1、首先明確一點:分組發生在where之後,即分組是基於where之後得到的記錄而進行的

#2、分組指的是:將所有記錄按照某個相同欄位進行歸類,比如針對員工資訊表的職位分組,或者按照性別進行分組等

#3、為何要分組呢?
取每個部門的最高工資
取每個部門的員工數
取男人數和女人數

小竅門:‘每'這個字後面的欄位,就是我們分組的依據

#4、大前提:

可以按照任意欄位分組,但是分組完畢後,比如group by post,只能檢視post欄位,如果想檢視組內資訊,需要藉助於聚合函式

當執行以下sql語句的時候,沒有報錯,但本身是沒有意義的

mysql> select * from employee 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                 |       | 1000000.31 |  401 |     1 |
| 1 | egon  | male  | 18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使       | NULL     |  7300.33 |  401 |     1 |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
4 rows in set (0.00 sec)

設定sql_mode為ONLY_FULL_GROUP_BY,並且退出,再進入才會生效

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

再次進入

mysql> select @@sql_mode;
+-----------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------

mysql> select * from emp group by post;//現在的情況下就會報錯
ERROR 1054 (42S22): Unknown column 'post' in 'group statement'
mysql> select * from employee group by post;
ERROR 1055 (42000): 't1.employee.id' isn't in GROUP BY
mysql> select post from employee group by post;
+-----------------------------------------+
| post                  |
+-----------------------------------------+
| operation                |
| sale                  |
| teacher                 |
| 老男孩駐沙河辦事處外交大使       |
+-----------------------------------------+
4 rows in set (0.00 sec)

或者如下使用

mysql> select name,post from employee group by post,name;
+------------+-----------------------------------------+
| name    | post                  |
+------------+-----------------------------------------+
| 張野    | operation                |
| 程咬金   | operation                |
| 程咬鐵   | operation                |
| 程咬銅   | operation                |
| 程咬銀   | operation                |
| 丁丁    | sale                  |
| 丫丫    | sale                  |
| 星星    | sale                  |
| 格格    | sale                  |
| 歪歪    | sale                  |
| alex    | teacher                 |
| jingliyang | teacher                 |
| jinxin   | teacher                 |
| liwenzhou | teacher                 |
| wupeiqi  | teacher                 |
| xiaomage  | teacher                 |
| yuanhao  | teacher                 |
| egon    | 老男孩駐沙河辦事處外交大使       |
+------------+-----------------------------------------+
18 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)

(3)聚合函式

max()求最大值
min()求最小值
avg()求平均值
sum() 求和
count() 求總個數

#強調:聚合函式聚合的是組的內容,若是沒有分組,則預設一組
# 每個部門有多少個員工
select post,count(id) from employee group by post;
# 每個部門的最高薪水
select post,max(salary) from employee group by post;
# 每個部門的最低薪水
select post,min(salary) from employee group by post;
# 每個部門的平均薪水
select post,avg(salary) from employee group by post;
# 每個部門的所有薪水
select post,sum(age) from employee group by post;

(4)HAVING過濾

HAVING與WHERE不一樣的地方在於

#!!!執行優先順序從高到低:where > group by > having
#1. Where 發生在分組group by之前,因而Where中可以有任意欄位,但是絕對不能使用聚合函式。

#2. Having發生在分組group by之後,因而Having中可以使用分組的欄位,無法直接取到其他欄位,可以使用聚合函式

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

mysql> select * from employee having salary>1000000;
ERROR 1463 (42000): Non-grouping field 'salary' is used in HAVING clause

# 必須使用group by才能使用group_concat()函式,將所有的name值連線
mysql> select post,group_concat(name) from emp group by post having salary > 10000; ##錯誤,分組後無法直接取到salary欄位
ERROR 1054 (42S22): Unknown column 'post' in 'field list'

練習

1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
2. 查詢各崗位平均薪資大於10000的崗位名、平均工資
3. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資

答案

mysql> select post,group_concat(name),count(id) from employee group by post;
+-----------------------------------------+-----------------------------------------------------------+-----------+
| post                  | group_concat(name)                    | count(id) |
+-----------------------------------------+-----------------------------------------------------------+-----------+
| operation                | 程咬鐵,程咬銅,程咬銀,程咬金,張野             |     5 |
| sale                  | 格格,星星,丁丁,丫丫,歪歪                 |     5 |
| teacher                 | xiaomage,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex |     7 |
| 老男孩駐沙河辦事處外交大使       | egon                           |     1 |
+-----------------------------------------+-----------------------------------------------------------+-----------+
4 rows in set (0.00 sec)

mysql> select post,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:
mysql> select post,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:
mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;
+-----------+--------------+
| post   | avg(salary) |
+-----------+--------------+
| operation | 16800.026000 |
+-----------+--------------+
1 row in set (0.00 sec)

(5)order by 查詢排序

按單列排序

  SELECT * FROM employee ORDER BY age;
  SELECT * FROM employee ORDER BY age ASC;
  SELECT * FROM employee ORDER BY age DESC;

按多列排序:先按照age升序排序,如果年紀相同,則按照id降序

  SELECT * from employee
    ORDER BY age ASC,id DESC;

(5)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條

練習:每次顯示5條

# 第1頁資料
 mysql> select * from employee limit 0,5;
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 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                 |       | 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 |
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
5 rows in set (0.00 sec)
# 第2頁資料
mysql> select * from employee limit 5,5;
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| 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 |
| 8 | xiaomage  | 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 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec)
# 第3頁資料
mysql> select * from employee limit 10,5;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 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 |
| 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 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec)

更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL查詢技巧大全》、《MySQL常用函式大彙總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧彙總》、《MySQL儲存過程技巧大全》及《MySQL資料庫鎖相關技巧彙總》

希望本文所述對大家MySQL資料庫計有所幫助。