Order by
用途:
指定结果集的排序
语法:
SELECT column-name(s) FROM table-name ORDER BY { order_by_expression [ ASC | DESC ] } |
解释:
指定结果集的排序,可以按照ASC(递增方式排序,从最低值到最高值)或者DESC(递减方式排序,从最高值到最低值)的方式进行排序,默认的方式是ASC
例:
“Orders”表中的原始数据:
Company |
OrderNumber |
Sega |
3412 |
ABC Shop |
5678 |
W3Schools |
2312 |
W3Schools |
6798 |
按照Company字段的升序方式返回结果集:
SELECT Company, OrderNumber FROM Orders ORDER BY Company |
返回结果:
Company |
OrderNumber |
ABC Shop |
5678 |
Sega |
3412 |
W3Schools |
6798 |
W3Schools |
2312 |
按照Company字段的降序方式返回结果集:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC |
返回结果:
Company |
OrderNumber |
W3Schools |
6798 |
W3Schools |
2312 |
Sega |
3412 |
ABC Shop |
5678 |
Group by
用途:
对结果集进行分组,常与汇总函数一起使用。
语法:
SELECT column,SUM(column) FROM table GROUP BY column |
例:
“Sales”表中的原始数据:
Company |
Amount |
W3Schools |
5500 |
IBM |
4500 |
W3Schools |
7100 |
按照Company字段进行分组,求出每个Company的Amout的合计:
SELECT Company,SUM(Amount) FROM Sales GROUP BY Company |
返回结果:
Company |
SUM(Amount) |
W3Schools |
12600 |
IBM |
4500 |