ORDER對查詢結果進行排序

怪怪怪怪怪發表於2020-09-25

降序、升序 與隨機排序

  1. ASC 是升序,DESC是降序,預設是升序排列,注意,這裡是對查詢結果進行排序
  2. 按照年齡的升序, 降序排列
mysql> SELECT*FROM cms_user ORDER BY id ASC;
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| id | username  | password     | email       | regTime    | face     | proId | age  | sex     |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
|  1 | 張三      | zhangsan     | user@qq.com | 1419811708 | user.jpg |     1 | NULL | male    |
|  2 | 張三丰    | zhangsanfeng | user@qq.com | 1419812708 | user.jpg |     2 |   23 | female  |
|  3 | 章子怡    | zhangsan     | user@qq.com | 1419813708 | user.jpg |     3 |   46 | male    |
|  4 | long      | long         | user@qq.com | 1419814708 | user.jpg |     4 |   77 | female  |
|  5 | ring      | ring         | user@qq.com | 1419815708 | user.jpg |     2 |   54 | male    |
|  6 | queen     | queen        | user@qq.com | 1419861708 | user.jpg |     3 |   64 | female  |
|  7 | king      | king         | user@qq.com | 1419817708 | user.jpg |     5 |   28 | male    |
|  8 | blek      | blek         | user@qq.com | 1419818708 | user.jpg |     1 |   98 | female  |
|  9 | rose      | rose         | user@qq.com | 1419821708 | user.jpg |     2 |   27 | male    |
| 10 | lily      | lily         | user@qq.com | 1419831708 | user.jpg |     2 |   27 | female  |
| 11 | john      | john         | user@qq.com | 1419841708 | user.jpg |     2 |   31 | unknown |
| 12 | test1     | test1        | user@qq.com | 1419811708 | user.jpg |     1 |   60 | unknown |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
12 rows in set (0.00 sec)

mysql> SELECT*FROM cms_user ORDER BY id DESC;
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| id | username  | password     | email       | regTime    | face     | proId | age  | sex     |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| 12 | test1     | test1        | user@qq.com | 1419811708 | user.jpg |     1 |   60 | unknown |
| 11 | john      | john         | user@qq.com | 1419841708 | user.jpg |     2 |   31 | unknown |
| 10 | lily      | lily         | user@qq.com | 1419831708 | user.jpg |     2 |   27 | female  |
|  9 | rose      | rose         | user@qq.com | 1419821708 | user.jpg |     2 |   27 | male    |
|  8 | blek      | blek         | user@qq.com | 1419818708 | user.jpg |     1 |   98 | female  |
|  7 | king      | king         | user@qq.com | 1419817708 | user.jpg |     5 |   28 | male    |
|  6 | queen     | queen        | user@qq.com | 1419861708 | user.jpg |     3 |   64 | female  |
|  5 | ring      | ring         | user@qq.com | 1419815708 | user.jpg |     2 |   54 | male    |
|  4 | long      | long         | user@qq.com | 1419814708 | user.jpg |     4 |   77 | female  |
|  3 | 章子怡    | zhangsan     | user@qq.com | 1419813708 | user.jpg |     3 |   46 | male    |
|  2 | 張三丰    | zhangsanfeng | user@qq.com | 1419812708 | user.jpg |     2 |   23 | female  |
|  1 | 張三      | zhangsan     | user@qq.com | 1419811708 | user.jpg |     1 | NULL | male    |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
12 rows in set (0.00 sec)
  1. 使用欄位的位置按照降序排列
    這裡我用了order by 7 意思就是按照proId的升序排列的
mysql> SELECT*FROM cms_user ORDER BY 7;
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| id | username  | password     | email       | regTime    | face     | proId | age  | sex     |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
|  1 | 張三      | zhangsan     | user@qq.com | 1419811708 | user.jpg |     1 | NULL | male    |
|  8 | blek      | blek         | user@qq.com | 1419818708 | user.jpg |     1 |   98 | female  |
| 12 | test1     | test1        | user@qq.com | 1419811708 | user.jpg |     1 |   60 | unknown |
|  2 | 張三丰    | zhangsanfeng | user@qq.com | 1419812708 | user.jpg |     2 |   23 | female  |
|  5 | ring      | ring         | user@qq.com | 1419815708 | user.jpg |     2 |   54 | male    |
|  9 | rose      | rose         | user@qq.com | 1419821708 | user.jpg |     2 |   27 | male    |
| 10 | lily      | lily         | user@qq.com | 1419831708 | user.jpg |     2 |   27 | female  |
| 11 | john      | john         | user@qq.com | 1419841708 | user.jpg |     2 |   31 | unknown |
|  3 | 章子怡    | zhangsan     | user@qq.com | 1419813708 | user.jpg |     3 |   46 | male    |
|  6 | queen     | queen        | user@qq.com | 1419861708 | user.jpg |     3 |   64 | female  |
|  4 | long      | long         | user@qq.com | 1419814708 | user.jpg |     4 |   77 | female  |
|  7 | king      | king         | user@qq.com | 1419817708 | user.jpg |     5 |   28 | male    |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
  1. 按照多個欄位排
  • 按照年齡升序,id降序來排列
  • 會先考慮第一個欄位的排序,再在第一個欄位相同的情況下按照第二個欄位來排序
mysql> SELECT*FROM cms_user ORDER BY proId, age DESC;
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| id | username  | password     | email       | regTime    | face     | proId | age  | sex     |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
|  8 | blek      | blek         | user@qq.com | 1419818708 | user.jpg |     1 |   98 | female  |
| 12 | test1     | test1        | user@qq.com | 1419811708 | user.jpg |     1 |   60 | unknown |
|  1 | 張三      | zhangsan     | user@qq.com | 1419811708 | user.jpg |     1 | NULL | male    |
|  5 | ring      | ring         | user@qq.com | 1419815708 | user.jpg |     2 |   54 | male    |
| 11 | john      | john         | user@qq.com | 1419841708 | user.jpg |     2 |   31 | unknown |
|  9 | rose      | rose         | user@qq.com | 1419821708 | user.jpg |     2 |   27 | male    |
| 10 | lily      | lily         | user@qq.com | 1419831708 | user.jpg |     2 |   27 | female  |
|  2 | 張三丰    | zhangsanfeng | user@qq.com | 1419812708 | user.jpg |     2 |   23 | female  |
|  6 | queen     | queen        | user@qq.com | 1419861708 | user.jpg |     3 |   64 | female  |
|  3 | 章子怡    | zhangsan     | user@qq.com | 1419813708 | user.jpg |     3 |   46 | male    |
|  4 | long      | long         | user@qq.com | 1419814708 | user.jpg |     4 |   77 | female  |
|  7 | king      | king         | user@qq.com | 1419817708 | user.jpg |     5 |   28 | male    |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
12 rows in set (0.00 sec)
  1. 可以通過ORDER BY RAND()進行隨機排序
mysql> SELECT*FROM cms_user ORDER BY RAND();
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| id | username  | password     | email       | regTime    | face     | proId | age  | sex     |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
| 10 | lily      | lily         | user@qq.com | 1419831708 | user.jpg |     2 |   27 | female  |
| 12 | test1     | test1        | user@qq.com | 1419811708 | user.jpg |     1 |   60 | unknown |
|  6 | queen     | queen        | user@qq.com | 1419861708 | user.jpg |     3 |   64 | female  |
|  7 | king      | king         | user@qq.com | 1419817708 | user.jpg |     5 |   28 | male    |
|  2 | 張三丰    | zhangsanfeng | user@qq.com | 1419812708 | user.jpg |     2 |   23 | female  |
|  3 | 章子怡    | zhangsan     | user@qq.com | 1419813708 | user.jpg |     3 |   46 | male    |
|  9 | rose      | rose         | user@qq.com | 1419821708 | user.jpg |     2 |   27 | male    |
| 11 | john      | john         | user@qq.com | 1419841708 | user.jpg |     2 |   31 | unknown |
|  8 | blek      | blek         | user@qq.com | 1419818708 | user.jpg |     1 |   98 | female  |
|  1 | 張三      | zhangsan     | user@qq.com | 1419811708 | user.jpg |     1 | NULL | male    |
|  5 | ring      | ring         | user@qq.com | 1419815708 | user.jpg |     2 |   54 | male    |
|  4 | long      | long         | user@qq.com | 1419814708 | user.jpg |     4 |   77 | female  |
+----+-----------+--------------+-------------+------------+----------+-------+------+---------+
12 rows in set (0.01 sec)

總結

1. SELECT

  • SELECT 欄位名稱 FROM tbl_name
  • 運用AS 給欄位或是table起別名

2. WHERE

  • 新增比較倆條件例如>=, <=, !=,
  • 新增指定範圍, BETWEEN AND
  • 新增出現的集合 IN
  • 模糊查詢 LIKE
  • 多條件查詢 AND, OR

3. GROUP BY

  • GROUP BY 指的是分組
  • 按欄位分組
  • 按欄位位置分組
  • 聚合函式 GROUP_CONCAT()
    – 顯示某個欄位分組下的詳情
  • COUNT()
    – 查詢每個分組中的總人數
    – 欄位有空值時,不統計空值
  • MAX()
  • MIN()
  • AVG()
  • SUM()
  • ROLLUP
    – 對分組後的每一列進行總結

4. HAVING

  • 對分組進行二次篩選

5. ORDER BY

  • 對分組進行升,降,或亂序的排列
mysql> SELECT id,age,sex, GROUP_CONCAT(username) AS username,
    -> COUNT(*) AS totalusers,
    -> SUM(age) AS Age
    -> FROM cms_user
    -> GROUP BY sex
    -> HAVING COUNT(*)>=2
    -> ORDER BY age DESC, id ASC;
+----+------+---------+---------------------------------+------------+------+
| id | age  | sex     | username                        | totalusers | Age  |
+----+------+---------+---------------------------------+------------+------+
|  2 |   23 | female  | 張三丰,long,queen,blek,lily     |          5 |  289 |
|  1 | NULL | male    | 張三,章子怡,ring,king,rose      |          5 |  155 |
| 11 |   31 | unknown | john,test1                      |          2 |   91 |
+----+------+---------+---------------------------------+------------+------+
3 rows in set (0.00 sec)

相關文章