Sorting 排序

  1. ORDER BY ASC 升序,可以使用任何一個欄位排序。
    MariaDB root@(none):dyw> select * from tbl order by id asc;
    +----+-------+--------+-----------------+
    | id | title | author | submission_date |
    +----+-------+--------+-----------------+
    | 1  | c++   | peter  | 2023-03-30      |
    | 2  | Linux | linux  | 2023-01-30      |
    +----+-------+--------+-----------------+
    
    2 rows in set
    Time: 0.064s
    
  2. ORDER BY DESC 降序。
    MariaDB root@(none):dyw> select * from tbl order by id desc;
    +----+-------+--------+-----------------+
    | id | title | author | submission_date |
    +----+-------+--------+-----------------+
    | 2  | Linux | linux  | 2023-01-30      |
    | 1  | c++   | peter  | 2023-03-30      |
    +----+-------+--------+-----------------+
    
    2 rows in set
    Time: 0.045s
    
  3. 新增二筆紀錄。
    MariaDB root@(none):dyw> insert into tbl (title,author,submission_date) values("a
                          -> nsible","dywang","2023-04-30");
    Query OK, 1 row affected
    Time: 0.077s
    
    MariaDB root@(none):dyw> insert into tbl (title,author,submission_date) values(
                          -> "ansible","ben","2023-05-02");
    Query OK, 1 row affected
    Time: 0.047s
    
  4. 查詢 tbl 表格,有 4 筆紀錄。
    MariaDB root@(none):dyw> select * from tbl;
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | c++     | peter  | 2023-03-30      |
    | 2  | Linux   | linux  | 2023-01-30      |
    | 4  | ansible | dywang | 2023-04-30      |
    | 5  | ansible | ben    | 2023-05-02      |
    +----+---------+--------+-----------------+
    
    4 rows in set
    Time: 0.040s
    
  5. 先 title desc 降序排序,title 相同的再與 author asc 升序排列。
    MariaDB root@(none):dyw> select * from tbl order by title desc, author asc;
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 2  | Linux   | linux  | 2023-01-30      |
    | 1  | c++     | peter  | 2023-03-30      |
    | 5  | ansible | ben    | 2023-05-02      |
    | 4  | ansible | dywang | 2023-04-30      |
    +----+---------+--------+-----------------+
    
    4 rows in set
    Time: 0.031s
    
  6. 先 title desc 降序排序,title 相同的再與 author desc 降序排列。
    MariaDB root@(none):dyw> select * from tbl order by title desc, author desc;
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 2  | Linux   | linux  | 2023-01-30      |
    | 1  | c++     | peter  | 2023-03-30      |
    | 4  | ansible | dywang | 2023-04-30      |
    | 5  | ansible | ben    | 2023-05-02      |
    +----+---------+--------+-----------------+
    
    4 rows in set
    Time: 0.049s