SQL ORDER BY 子句
ORDER BY 語句用於根據指定的欄位對結果集進行排序。
ORDER BY 語句預設會依照升序對記錄進行排序,如需依照降序排序,請使用 DESC 關鍵字。
"Orders" 表:
Company OrderNumber IBM 3532 HuluMiao 2356 Apple 4698 IBM 6953
實例1:以字母順序顯示公司名稱:
SELECT Company, OrderNumber FROM Orders ORDER BY Company
結果:
Company OrderNumber Apple 4698 HuluMiao 2356 IBM 6953 IBM 3532
實例2 :以字母順序顯示公司名稱(Company),並以數字順序顯示順序號(OrderNumber):
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
結果:
Company OrderNumber Apple 4698 HuluMiao 2356 IBM 3532 IBM 6953
實例3 :以逆字母順序顯示公司名稱:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
結果:
Company OrderNumber IBM 6953 IBM 3532 HuluMiao 2356 Apple 4698
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
Company OrderNumber IBM 3532 IBM 6953 HuluMiao 2356 Apple 4698
以上是SQL中ORDER BY子句怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!