我有状态栏
的表格CREATE TABLE movies
(
id int NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL default '',
status int NOT NULL
);
状态可以在1到4之间 我想按状态
对结果进行排序我可以通过status
asc
和desc
轻松对查询进行排序,并在顶部获取值为1和4的行
$sql = "SELECT * FROM tbl order by `status` ASC";
但是如何在结果的顶部获得2和3?
答案 0 :(得分:2)
select *
from tbl
order by case when status in (2, 3) then 0 else 1 end,
`status`
答案 1 :(得分:0)
SELECT * FROM tbl where status IN (2,3) order by `status` ASC
UNION
SELECT * FROM tbl where status IN (1,4) order by `status` ASC
答案 2 :(得分:0)
您可以使用CASE
(作为RedFilter的回答)或 FIELD()
功能:
SELECT *
FROM tbl
ORDER BY FIELD(status, 2, 3, 1, 4) --- the order you prefer