English | 正體中文 | 简体中文

SQL order by


范例资料表 : Orders (以下范例皆用此表当作资料来源)
OrderNo CustomerName Shipped SalePrice Quantity
20101213095 John True 13500 10
20100620135 Jimmy False 25096 22
20100620392 Vincent False 7200 5
20100530006 Tyler True 92500 50
20100515029 Alan False 100 50


Example
指定选择出的资料列排序方式

Sytax : SELECT * FROM table_name ORDER BY sort_column_name
SQL
SELECT * FROM Orders ORDER BY SalePrice

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20100515029 Alan False 100 50
20100620392 Vincent False 7200 5
20101213095 John True 13500 10
20100620135 Jimmy False 25096 22
20100530006 Tyler True 92500 50


Example
让选择出的资料列以降幂排序

Sytax : SELECT * FROM table_name ORDER BY sort_column_name DESC
SQL
SELECT * FROM Orders ORDER BY Quantity DESC

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20100530006 Tyler True 92500 50
20100515029 Alan False 100 50
20100620135 Jimmy False 25096 22
20101213095 John True 13500 10
20100620392 Vincent False 7200 5


Example
让选择出的资料列以升幂排序

Sytax : SELECT * FROM table_name ORDER BY sort_column_name ASC
SQL
SELECT * FROM Orders ORDER BY Quantity ASC

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20100620392 Vincent False 7200 5
20101213095 John True 13500 10
20100620135 Jimmy False 25096 22
20100515029 Alan False 100 50
20100530006 Tyler True 92500 50


Example
让选择出的资料列以指定的多个栏位排序

Sytax : SELECT * FROM table_name ORDER BY sort_column_name1 , sort_column_name2 ...
SQL
SELECT * FROM Orders ORDER BY Quantity, Shipped

会输出结果
OrderNo CustomerName Shipped SalePrice Quantity
20100620392 Vincent False 7200 5
20101213095 John True 13500 10
20100620135 Jimmy False 25096 22
20100530006 Tyler True 92500 50
20100515029 Alan False 100 50

© 2012 PureExample.com contact