SQL ORDER BY 關鍵字
SQL ORDER BY
SQL ORDER BY
關鍵字用於按升序或降序對結果集進行排序。
語法
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
演示資料庫
下面是從示例中使用的 Products 表中選擇的一部分
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
DESC
ORDER BY
關鍵字預設按升序對記錄進行排序。要按降序對記錄進行排序,請使用 DESC
關鍵字。
按字母順序排序
對於字串值,ORDER BY
關鍵字將按字母順序排序。
反向字母順序
要反向排序表,請使用 DESC
關鍵字。
按多個列排序
以下 SQL 語句從 "Customers" 表中選擇所有客戶,並按 "Country" 和 "CustomerName" 列排序。這意味著它首先按國家排序,如果某些行的國家相同,則按客戶名稱排序。
同時使用 ASC 和 DESC
以下 SQL 語句從 "Customers" 表中選擇所有客戶,按 "Country" 升序排序,按 "CustomerName" 降序排序。
影片:SQL ORDER BY 關鍵字

