next up previous contents
Next: PHP 排序 Up: Clause 紀錄詢問條件 Previous: Clause 紀錄詢問條件   Contents   DYWANG_HOME

Sorting 排序

  1. ORDER BY ASC 升序,可以使用任何一個欄位排序。
    mariadb root@localhost:dyw> select * from tbl order by id asc;                  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | c++     | peter  | 2020-01-28      |
    | 2  | MariaDB | dywang | 2020-01-01      |
    +----+---------+--------+-----------------+
    
    2 rows in set
    Time: 0.031s
    
  2. ORDER BY DESC 降序。
    mariadb root@localhost:dyw> select * from tbl order by id desc;                 
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 1  | c++     | peter  | 2020-01-28      |
    +----+---------+--------+-----------------+
    
    2 rows in set
    Time: 0.032s
    
  3. 新增一筆紀錄。
    mariadb root@localhost:dyw> insert into tbl (title,author,submission_date) value
                                s("c++","dywang","2020-01-11");                     
    Query OK, 1 row affected
    Time: 0.006s
    
  4. 先 title desc 降序排序,title 相同的再與 author desc 降序排列。
    mariadb root@localhost:dyw> select * from tbl order by title desc, author desc; 
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 1  | c++     | peter  | 2020-01-28      |
    | 4  | c++     | dywang | 2020-01-11      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.033s
    
  5. 先 title desc 降序排序,title 相同的再與 author asc 升序排列。
    mariadb root@localhost:dyw> select * from tbl order by title desc, author asc;  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 4  | c++     | dywang | 2020-01-11      |
    | 1  | c++     | peter  | 2020-01-28      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.032s
    



De-Yu Wang 2020-04-07