SQL ORDER BY 关键字

SQL ORDER BY 关键字 #

ORDER BY 关键字用于对结果集进行排序。

ORDER BY 关键字用于对结果集按照一个列或者多个列进行排序。

ORDER BY 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 DESC 关键字。

SQL ORDER BY 语法

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

ORDER BY 实例 #

下面的 SQL 语句从 “domains” 表中选取所有记录,并按照 “icp” 列排序:

MariaDB [learnsql]> select * from domains order by icp;
+----+-----------+------------------+------------+------------+
| ID | ICP       | DomainName       | RegDate    | EndDate    |
+----+-----------+------------------+------------+------------+
| 43 | dnspod.cn | zjq.xyz          | 2021-08-06 | 2022-10-24 |
| 50 | dnspod.cn | byhzg.cn         | 2022-01-28 | 2023-04-14 |
| 49 | dnspod.cn | gfwlist.cn       | 2022-01-28 | 2023-03-23 |
| 46 | dnspod.cn | nasplus.cn       | 2022-01-28 | 2023-03-08 |
| 48 | dnspod.cn | pushto.cn        | 2022-01-28 | 2023-03-12 |
| 47 | dnspod.cn | manjushri.cn     | 2022-01-28 | 2023-03-11 |
| 44 | dnspod.cn | alair.cn         | 2021-08-06 | 2022-12-10 |
| 45 | dnspod.cn | ymqd.net         | 2022-01-28 | 2023-03-03 |
| 52 | gandi.net | mua.wiki         | 2022/7/19  | 2023/7/19  |
| 22 | google    | suopo.net        | 2020/6/17  | 2023/6/17  |
| 21 | namesilo  | avlist.top       | 2021/6/5   | 2023/6/5   |
| 20 | namesilo  | ciux.org         | 2010/4/26  | 2023/4/26  |
| 18 | namesilo  | ustv.xyz         | 2018/3/24  | 2023/3/25  |
| 25 | namesilo  | pornie.top       | 2016/3/10  | 2027/3/10  |
| 16 | namesilo  | bailuyuan.org    | 2017/3/23  | 2023/3/23  |
| 15 | namesilo  | aosp.me          | 2016/3/17  | 2023/3/17  |
| 14 | namesilo  | pornie.in        | 2016/3/10  | 2023/3/10  |
| 13 | namesilo  | raspi.in         | 2014/2/27  | 2023/2/27  |
| 12 | namesilo  | blbl.dev         | 2020/1/17  | 2023/1/17  |
|  9 | namesilo  | regex.in         | 2020/1/3   | 2023/1/3   |
|  8 | namesilo  | les.moe          | 2015/12/31 | 2022/12/31 |
|  4 | namesilo  | getos.org        | 2016/10/9  | 2022/10/9  |
| 24 | namesilo  | ltecn.com        | 2014/7/26  | 2025/7/26  |
|  7 | wanwang   | lyq.wiki         | 2018/12/27 | 2022/12/28 |
| 17 | wanwang   | linuxuc.com      | 2016/3/24  | 2023/3/24  |
| 19 | wanwang   | unixetc.com      | 2017/3/30  | 2023/3/30  |
|  6 | wanwang   | dotbbq.com       | 2017/10/28 | 2022/10/28 |
| 41 | west.cn   | qgis.top         | 2021/8/23  | 2031/8/23  |
| 42 | west.cn   | hugotheme.top    | 2022/2/18  | 2032/2/18  |
|  1 | west.cn   | aisoc.cn         | 2021/8/17  | 2022/8/17  |
| 11 | west.cn   | sharpbang.cn     | 2022/1/12  | 2023/1/12  |
| 10 | west.cn   | wmts.top         | 2021/1/11  | 2023/1/11  |
| 27 | west.cn   | imagemagick.top  | 2017/10/24 | 2027/10/24 |
|  5 | west.cn   | litiaotiao.cn    | 2021/10/9  | 2022/10/9  |
|  3 | west.cn   | nrdoc.com        | 2021/9/7   | 2022/9/7   |
|  2 | west.cn   | ado.ink          | 2020/8/22  | 2022/8/22  |
| 51 | west.cn   | guwendaquan.cn   | 2022/6/9   | 2023/6/9   |
| 23 | west.cn   | vbar.xyz         | 2020/7/20  | 2023/7/20  |
| 39 | west.cn   | autohotkey.top   | 2021/3/3   | 2031/3/3   |
| 38 | west.cn   | rgbs.top         | 2021/2/25  | 2031/2/25  |
| 26 | west.cn   | sjcs.top         | 2017/9/5   | 2027/9/5   |
| 28 | west.cn   | oppa.top         | 2015/12/29 | 2027/12/29 |
| 29 | west.cn   | kodis.top        | 2018/1/25  | 2028/1/25  |
| 30 | west.cn   | vpshz.top        | 2018/7/18  | 2028/7/18  |
| 31 | west.cn   | smdm.top         | 2018/9/10  | 2028/9/10  |
| 32 | west.cn   | commandline.top  | 2020/10/21 | 2029/10/21 |
| 33 | west.cn   | wxtx.top         | 2020/8/11  | 2030/8/11  |
| 34 | west.cn   | kjzx.top         | 2020/8/16  | 2030/8/16  |
| 35 | west.cn   | aido.cc          | 2020/9/22  | 2030/9/22  |
| 36 | west.cn   | ustv.top         | 2020/10/13 | 2030/10/13 |
| 37 | west.cn   | colorhexcode.top | 2021/1/27  | 2031/1/27  |
| 40 | west.cn   | opensuse.top     | 2021/3/25  | 2031/3/25  |
+----+-----------+------------------+------------+------------+
52 rows in set (0.001 sec)

ORDER BY DESC 实例 #

下面的 SQL 语句从 “domains” 表中选取所有记录,并按照 “domainname” 列降序排序:

MariaDB [learnsql]> select * from domains order by domainname desc;
+----+-----------+------------------+------------+------------+
| ID | ICP       | DomainName       | RegDate    | EndDate    |
+----+-----------+------------------+------------+------------+
| 43 | dnspod.cn | zjq.xyz          | 2021-08-06 | 2022-10-24 |
| 45 | dnspod.cn | ymqd.net         | 2022-01-28 | 2023-03-03 |
| 33 | west.cn   | wxtx.top         | 2020/8/11  | 2030/8/11  |
| 10 | west.cn   | wmts.top         | 2021/1/11  | 2023/1/11  |
| 30 | west.cn   | vpshz.top        | 2018/7/18  | 2028/7/18  |
| 23 | west.cn   | vbar.xyz         | 2020/7/20  | 2023/7/20  |
| 18 | namesilo  | ustv.xyz         | 2018/3/24  | 2023/3/25  |
| 36 | west.cn   | ustv.top         | 2020/10/13 | 2030/10/13 |
| 19 | wanwang   | unixetc.com      | 2017/3/30  | 2023/3/30  |
| 22 | google    | suopo.net        | 2020/6/17  | 2023/6/17  |
| 31 | west.cn   | smdm.top         | 2018/9/10  | 2028/9/10  |
| 26 | west.cn   | sjcs.top         | 2017/9/5   | 2027/9/5   |
| 11 | west.cn   | sharpbang.cn     | 2022/1/12  | 2023/1/12  |
| 38 | west.cn   | rgbs.top         | 2021/2/25  | 2031/2/25  |
|  9 | namesilo  | regex.in         | 2020/1/3   | 2023/1/3   |
| 13 | namesilo  | raspi.in         | 2014/2/27  | 2023/2/27  |
| 41 | west.cn   | qgis.top         | 2021/8/23  | 2031/8/23  |
| 48 | dnspod.cn | pushto.cn        | 2022-01-28 | 2023-03-12 |
| 25 | namesilo  | pornie.top       | 2016/3/10  | 2027/3/10  |
| 14 | namesilo  | pornie.in        | 2016/3/10  | 2023/3/10  |
| 28 | west.cn   | oppa.top         | 2015/12/29 | 2027/12/29 |
| 40 | west.cn   | opensuse.top     | 2021/3/25  | 2031/3/25  |
|  3 | west.cn   | nrdoc.com        | 2021/9/7   | 2022/9/7   |
| 46 | dnspod.cn | nasplus.cn       | 2022-01-28 | 2023-03-08 |
| 52 | gandi.net | mua.wiki         | 2022/7/19  | 2023/7/19  |
| 47 | dnspod.cn | manjushri.cn     | 2022-01-28 | 2023-03-11 |
|  7 | wanwang   | lyq.wiki         | 2018/12/27 | 2022/12/28 |
| 24 | namesilo  | ltecn.com        | 2014/7/26  | 2025/7/26  |
|  5 | west.cn   | litiaotiao.cn    | 2021/10/9  | 2022/10/9  |
| 17 | wanwang   | linuxuc.com      | 2016/3/24  | 2023/3/24  |
|  8 | namesilo  | les.moe          | 2015/12/31 | 2022/12/31 |
| 29 | west.cn   | kodis.top        | 2018/1/25  | 2028/1/25  |
| 34 | west.cn   | kjzx.top         | 2020/8/16  | 2030/8/16  |
| 27 | west.cn   | imagemagick.top  | 2017/10/24 | 2027/10/24 |
| 42 | west.cn   | hugotheme.top    | 2022/2/18  | 2032/2/18  |
| 51 | west.cn   | guwendaquan.cn   | 2022/6/9   | 2023/6/9   |
| 49 | dnspod.cn | gfwlist.cn       | 2022-01-28 | 2023-03-23 |
|  4 | namesilo  | getos.org        | 2016/10/9  | 2022/10/9  |
|  6 | wanwang   | dotbbq.com       | 2017/10/28 | 2022/10/28 |
| 32 | west.cn   | commandline.top  | 2020/10/21 | 2029/10/21 |
| 37 | west.cn   | colorhexcode.top | 2021/1/27  | 2031/1/27  |
| 20 | namesilo  | ciux.org         | 2010/4/26  | 2023/4/26  |
| 50 | dnspod.cn | byhzg.cn         | 2022-01-28 | 2023-04-14 |
| 12 | namesilo  | blbl.dev         | 2020/1/17  | 2023/1/17  |
| 16 | namesilo  | bailuyuan.org    | 2017/3/23  | 2023/3/23  |
| 21 | namesilo  | avlist.top       | 2021/6/5   | 2023/6/5   |
| 39 | west.cn   | autohotkey.top   | 2021/3/3   | 2031/3/3   |
| 15 | namesilo  | aosp.me          | 2016/3/17  | 2023/3/17  |
| 44 | dnspod.cn | alair.cn         | 2021-08-06 | 2022-12-10 |
|  1 | west.cn   | aisoc.cn         | 2021/8/17  | 2022/8/17  |
| 35 | west.cn   | aido.cc          | 2020/9/22  | 2030/9/22  |
|  2 | west.cn   | ado.ink          | 2020/8/22  | 2022/8/22  |
+----+-----------+------------------+------------+------------+
52 rows in set (0.001 sec)

ORDER BY 多列 #

下面的 SQL 语句从 “domains” 表中选取所有网站,并按照 “icp” 和 “domainname” 列排序:

MariaDB [learnsql]> select * from domains order by icp,domainname;
+----+-----------+------------------+------------+------------+
| ID | ICP       | DomainName       | RegDate    | EndDate    |
+----+-----------+------------------+------------+------------+
| 44 | dnspod.cn | alair.cn         | 2021-08-06 | 2022-12-10 |
| 50 | dnspod.cn | byhzg.cn         | 2022-01-28 | 2023-04-14 |
| 49 | dnspod.cn | gfwlist.cn       | 2022-01-28 | 2023-03-23 |
| 47 | dnspod.cn | manjushri.cn     | 2022-01-28 | 2023-03-11 |
| 46 | dnspod.cn | nasplus.cn       | 2022-01-28 | 2023-03-08 |
| 48 | dnspod.cn | pushto.cn        | 2022-01-28 | 2023-03-12 |
| 45 | dnspod.cn | ymqd.net         | 2022-01-28 | 2023-03-03 |
| 43 | dnspod.cn | zjq.xyz          | 2021-08-06 | 2022-10-24 |
| 52 | gandi.net | mua.wiki         | 2022/7/19  | 2023/7/19  |
| 22 | google    | suopo.net        | 2020/6/17  | 2023/6/17  |
| 15 | namesilo  | aosp.me          | 2016/3/17  | 2023/3/17  |
| 21 | namesilo  | avlist.top       | 2021/6/5   | 2023/6/5   |
| 16 | namesilo  | bailuyuan.org    | 2017/3/23  | 2023/3/23  |
| 12 | namesilo  | blbl.dev         | 2020/1/17  | 2023/1/17  |
| 20 | namesilo  | ciux.org         | 2010/4/26  | 2023/4/26  |
|  4 | namesilo  | getos.org        | 2016/10/9  | 2022/10/9  |
|  8 | namesilo  | les.moe          | 2015/12/31 | 2022/12/31 |
| 24 | namesilo  | ltecn.com        | 2014/7/26  | 2025/7/26  |
| 14 | namesilo  | pornie.in        | 2016/3/10  | 2023/3/10  |
| 25 | namesilo  | pornie.top       | 2016/3/10  | 2027/3/10  |
| 13 | namesilo  | raspi.in         | 2014/2/27  | 2023/2/27  |
|  9 | namesilo  | regex.in         | 2020/1/3   | 2023/1/3   |
| 18 | namesilo  | ustv.xyz         | 2018/3/24  | 2023/3/25  |
|  6 | wanwang   | dotbbq.com       | 2017/10/28 | 2022/10/28 |
| 17 | wanwang   | linuxuc.com      | 2016/3/24  | 2023/3/24  |
|  7 | wanwang   | lyq.wiki         | 2018/12/27 | 2022/12/28 |
| 19 | wanwang   | unixetc.com      | 2017/3/30  | 2023/3/30  |
|  2 | west.cn   | ado.ink          | 2020/8/22  | 2022/8/22  |
| 35 | west.cn   | aido.cc          | 2020/9/22  | 2030/9/22  |
|  1 | west.cn   | aisoc.cn         | 2021/8/17  | 2022/8/17  |
| 39 | west.cn   | autohotkey.top   | 2021/3/3   | 2031/3/3   |
| 37 | west.cn   | colorhexcode.top | 2021/1/27  | 2031/1/27  |
| 32 | west.cn   | commandline.top  | 2020/10/21 | 2029/10/21 |
| 51 | west.cn   | guwendaquan.cn   | 2022/6/9   | 2023/6/9   |
| 42 | west.cn   | hugotheme.top    | 2022/2/18  | 2032/2/18  |
| 27 | west.cn   | imagemagick.top  | 2017/10/24 | 2027/10/24 |
| 34 | west.cn   | kjzx.top         | 2020/8/16  | 2030/8/16  |
| 29 | west.cn   | kodis.top        | 2018/1/25  | 2028/1/25  |
|  5 | west.cn   | litiaotiao.cn    | 2021/10/9  | 2022/10/9  |
|  3 | west.cn   | nrdoc.com        | 2021/9/7   | 2022/9/7   |
| 40 | west.cn   | opensuse.top     | 2021/3/25  | 2031/3/25  |
| 28 | west.cn   | oppa.top         | 2015/12/29 | 2027/12/29 |
| 41 | west.cn   | qgis.top         | 2021/8/23  | 2031/8/23  |
| 38 | west.cn   | rgbs.top         | 2021/2/25  | 2031/2/25  |
| 11 | west.cn   | sharpbang.cn     | 2022/1/12  | 2023/1/12  |
| 26 | west.cn   | sjcs.top         | 2017/9/5   | 2027/9/5   |
| 31 | west.cn   | smdm.top         | 2018/9/10  | 2028/9/10  |
| 36 | west.cn   | ustv.top         | 2020/10/13 | 2030/10/13 |
| 23 | west.cn   | vbar.xyz         | 2020/7/20  | 2023/7/20  |
| 30 | west.cn   | vpshz.top        | 2018/7/18  | 2028/7/18  |
| 10 | west.cn   | wmts.top         | 2021/1/11  | 2023/1/11  |
| 33 | west.cn   | wxtx.top         | 2020/8/11  | 2030/8/11  |
+----+-----------+------------------+------------+------------+
52 rows in set (0.000 sec)