Sorting with ORDER BY · 用 ORDER BY 排序
Sorting the result
The rows come back in no particular order. Add ORDER BY to sort them by a column:
ASCsorts ascending (smallest first) — this is the default.DESCsorts descending (largest first).
给结果排序
返回的行没有固定顺序。加上 ORDER BY,按某一列把它们排序:
SELECT name, score FROM student ORDER BY score DESC;
ASC表示升序(从小到大)—— 这是默认值。DESC表示降序(从大到小)。
Sorting by more than one column
Sort text columns alphabetically:
You can sort by several columns: ORDER BY form ASC, score DESC sorts by form first, then by score within each form. ORDER BY always comes last in the query.
按多列排序
按文本列可以按字母排序:
SELECT name FROM student ORDER BY name;
你可以按多列排序:ORDER BY form ASC, score DESC 先按 form 排,再在每个 form 内部按 score 排。ORDER BY 总是写在查询的最后。
Common mistakes
- The default sort is ascending; add
DESCfor descending. ORDER BYcomes near the end of the query.
常见错误
- 默认升序排序;要降序加
DESC。 ORDER BY写在查询靠后的位置。
ORDER BY sorts the rows · ORDER BY 排序
ORDER BY puts the result rows in order, like a sort. · ORDER BY 把结果行排好序,就像排序算法。
Show every student's name and score, highest score first. · 显示每名学生的 name 和 score,分数最高的排在最前。
Click Run to see the output here. · 点击“运行”查看此处输出。
Show every student's name, sorted alphabetically (A to Z). · 显示每名学生的 name,按字母顺序(A 到 Z)排列。
Click Run to see the output here. · 点击“运行”查看此处输出。