SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
column1, column2, ...:要排序的字段名称,可以为多个字段。在本教程中,我们将使用 RUNOOB 样本数据库。
下面是选自 "Websites" 表的数据:
+----+--------------+---------------------------+-------+---------+
+----+--------------+---------------------------+-------+---------+
<table>
<tr>
<th>1</th>
<th>Google</th>
<th>https://www.google.cm/</th>
<th>1</th>
<th>USA</th>
</tr>
<tr>
<td>3</td>
<td>菜鸟教程</td>
<td>http://www.runoob.com/</td>
<td>4689</td>
<td>CN</td>
</tr>
<tr>
<td>4</td>
<td>微博</td>
<td>http://weibo.com/</td>
<td>20</td>
<td>CN</td>
</tr>
<tr>
<td>5</td>
<td>Facebook</td>
<td>https://www.facebook.com/</td>
<td>3</td>
<td>USA</td>
</tr>
</table>
+----+--------------+---------------------------+-------+---------+下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "alexa" 列排序:
SELECT * FROM Websites
ORDER BY alexa;下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "alexa" 列降序排序:
SELECT * FROM Websites
ORDER BY alexa DESC;下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "country" 和 "alexa" 列排序:
SELECT * FROM Websites
ORDER BY country,alexa;来源:https://www.runoob.com/sql/sql-orderby.html