在status
列中有以下值:slider
- sidebar
- below
- oncheck
- later
。
我希望得到所有项目的列表,但首先是status
= slider
,然后是sidebar
,依此类推。
每个status
组都应按date desc
排序。
我尝试了UNION
$sql = "select * from posts where status = 'slider' union
select * from posts where status = 'sidebar' union
select * from posts where status = 'below' union
select * from posts where status = 'oncheck' union
select * from posts where status = 'later'
order by date desc";
结果 - 项目混乱(首先below
然后slider
然后再below
等)
任何帮助?
答案 0 :(得分:2)
使用MySQL FIELD()函数:
SELECT *
FROM posts
ORDER BY FIELD(status, 'slider', 'sidebar', 'below', 'oncheck', 'later');
这将根据需要对行进行排序:)
答案 1 :(得分:1)
SELECT * FROM posts
ORDER BY FIELD (status, 'slider', 'sidebar'),date desc;
请检查一下。