MySQL學習 (三) Limit-Distinct-Union

MySQL學習 (三) Limit-Distinct-Union

Limit

簡介

  • 限制得到的記錄數量
mysql> SELECT * FROM teacher_class limit 1;
+----+--------+--------+-----------+------+------+------------+------------+
| id | t_name | gender | c_name    | room | days | begin_date | end_date   |
+----+--------+--------+-----------+------+------+------------+------------+
|  1 | Gene   | male   | python001 | 334  |   22 | 2013-03-31 | 2013-05-05 |
+----+--------+--------+-----------+------+------+------------+------------+
1 row in set (0.00 sec)

語法

Limit offset, row_count

# offset 偏移量,從0開始;
# row_count,總記錄數,長度,不是開始和結束, 若是數量大於餘下的記錄數,則獲取全部餘下的記錄數,默認值0
mysql> SELECT * FROM teacher_class limit 3,4;
+----+---------+--------+-----------+------+------+------------+------------+
| id | t_name  | gender | c_name    | room | days | begin_date | end_date   |
+----+---------+--------+-----------+------+------+------------+------------+
|  4 | Emma    | female | python004 | 338  |   20 | 2013-03-15 | 2013-04-05 |
|  5 | Janice  | female | python007 | 332  |   27 | 2013-04-15 | 2013-05-10 |
|  6 | Jessica | female | python008 | 332  |   28 | 2013-05-15 | 2013-06-08 |
+----+---------+--------+-----------+------+------+------------+------------+
3 rows in set (0.00 sec)

Distinct

簡介

  • distinct: 去掉重複記錄, 指的是字段值,都相同的記錄,而不是部分字段相同的記錄
  • All: 表示全部,默認就是all的行爲
mysql> SELECT days FROM teacher_class;
+------+
| days |
+------+
|   22 |
|   22 |
|   55 |
|   20 |
|   27 |
|   28 |
+------+
6 rows in set (0.00 sec)

mysql> SELECT DISTINCT days FROM teacher_class;
+------+
| days |
+------+
|   22 |
|   55 |
|   20 |
|   27 |
|   28 |
+------+
5 rows in set (0.00 sec)

mysql> SELECT days, begin_date FROM teacher_class;
+------+------------+
| days | begin_date |
+------+------------+
|   22 | 2013-03-31 |
|   22 | 2013-05-31 |
|   55 | 2013-02-15 |
|   20 | 2013-03-15 |
|   27 | 2013-04-15 |
|   28 | 2013-05-15 |
+------+------------+
6 rows in set (0.01 sec)

mysql> SELECT DISTINCT days, begin_date FROM teacher_class;
+------+------------+
| days | begin_date |
+------+------------+
|   22 | 2013-03-31 |
|   22 | 2013-05-31 |
|   55 | 2013-02-15 |
|   20 | 2013-03-15 |
|   27 | 2013-04-15 |
|   28 | 2013-05-15 |
+------+------------+
6 rows in set (0.00 sec)

union聯合查詢

簡介

  • 將多條select語句的結果,合併到一塊兒,稱爲聯合python

  • 使用union關鍵字,聯合兩個select語句便可mysql

  • 注意 若是union查詢結果有重複,會自動去掉重複的數據;若是想獲得所有數據,經過UNION ALL來顯示數據sql

  • 多個select語句檢索時,字段要一致; 數據類型,儘可能保持一致;若是不一致,應該能讓MySQL可以作類型轉換學習

  • 檢索結果列名稱的問題:根據第一條select語句的字段,進行名稱排列code

  • 場景
    1. 得到數據的條件,出現邏輯衝突,或者很難在一個邏輯內表示,很差拆分,分紅多個邏輯,分別實現,而後合併到一塊兒
mysql> SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ORDER BY days DESC LIMIT 1;
+---------+------+
| t_name  | days |
+---------+------+
| Jessica |   55 |
+---------+------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ORDER BY days DESC LIMIT 1;
+---------+------+
| t_name  | days |
+---------+------+
| Jessica |   28 |
+---------+------+
1 row in set (0.00 sec)

# 聯合查詢語法

mysql>(SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ORDER BY days DESC LIMIT 1) UNION (SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ORDER BY days DESC LIMIT 1);
+---------+------+
| t_name  | days |
+---------+------+
| Jessica |   55 |
| Jessica |   28 |
+---------+------+
2 rows in set (0.00 sec)

子語句的結果排序

  1. 將子語句包裹在括號裏
  2. 子語句的order by須要使用limit才能生效,不然,排序失效

全部結果進行排序

  1. 只須要在最後一個select子語句後,使用ORDER BY進行排序
mysql> (SELECT t_name, days FROM teacher_class WHERE c_name = "python002" ) \
    -> UNION (SELECT t_name, days FROM teacher_class WHERE c_name = "python008" ) ORDER BY days;
+---------+------+
| t_name  | days |
+---------+------+
| Mona    |   22 |
| Emma    |   28 |
| Jessica |   28 |
| Mona    |   55 |
| Jessica |   55 |
+---------+------+
5 rows in set (0.00 sec)
相關文章
相關標籤/搜索