ORDER BY 關鍵字 (SQL ORDER BY Keyword)
我們可以將 SELECT 取得的資料集依某欄位來作排序,而排序分別可以由小至大 (ascending; 預設),或由大至小 (descending)。
ORDER BY 語法 (SQL ORDER BY Syntax)
1 | SELECT table_column1, table_column2... |
Example 舉例
1 | SELECT `C_Id`, `Name`, `City`, `Address`, `Phone` FROM `customers` ORDER BY `Name`; |
1 | SELECT `C_Id`, `Name`, `City`, `Address`, `Phone` FROM `customers` ORDER BY `City`; |
1 | SELECT `C_Id`, `Name`, `City`, `Address`, `Phone` FROM `customers` ORDER BY `Address`; |
1 | SELECT `C_Id`, `Name`, `City`, `Address`, `Phone` FROM `customers` ORDER BY `Address`; |
1 | SELECT `C_Id`, `Name`, `City`, `Address`, `Phone` FROM `customers` ORDER BY `Phone`; |
ORDER BY 查詢用法 (Example)
從下面的 employees 資料表中取出所有員工的資料並依職稱來作排序 (即字母順序):
1 | SELECT * FROM `customers` ORDER BY `City`; |
若語句中沒加上 ASC 或 DESC 關鍵字,預設默認為 ASC。