1、单列排序
SELECT
*
FROM
table_name
ORDER BY
column_name;
ASC默认升序,降序后面接"DESC"即可。
2、多列排序
SELECT
*
FROM
table_name
ORDER BY
column_name1,
column_name2 DESC;
首先按`column_name1`字段排序,若`column_name1`相等,则按column_name2排序。
3、自定义排序
SELECT
*
FROM
table_name
ORDER BY
FIELD(`column_name1`, 0, 1, 5, 2),
column_name2 DESC;
使用"FIELD()"函数,可指定顺序。
4、其他条件排序
SELECT
*
FROM
table_name
ORDER BY
column_name1 < NOW(),
IF (
column_name1 < NOW(),
0,
column_name1
),
column_name1 DESC;
先按大于等于当前时间升序,再按小于当前时间降序,支持分页。
5、指定字段排序
SELECT
*
FROM
table_name
ORDER BY
column_name1 DESC,
column_name1 ASC;
先按column_name1降序排,再按column_name2升序排。