我有一个'book_history'表,其中包含字段'status'
So there are three values for the field 'status' => 0,1,2
现在我想按顺序查询它 - 我知道asc的顺序和desc的顺序就在那里。 但我真正想要的是
select * from book_history order by status 1,0,2
我当时检查了订单。但是我无法进行查询。
所以最终输出将是 - 首先它应该列出status = 1,然后status = 0然后status ='2'
任何帮助。
谢谢,
Kimz
答案 0 :(得分:3)
您可以使用FIELD()功能
SELECT
*
FROM
book_history
ORDER BY FIELD(`status`, 1, 0, 2)
答案 1 :(得分:3)
使用以下查询强制查询中的顺序:
select * from book_history
ORDER BY FIELD (status,1,0,2);
有关更多信息,如果有更多值,并且您将值保持在顶部,之后的值为其余值,则可以使用以下查询:
select * from book_history
ORDER BY FIELD (status,2,0,1) desc;
答案 2 :(得分:2)
您可以使用ORDER BY FIELD:
SELECT * FROM book_history ORDER BY FIELD(status, 1,0,2)
答案 3 :(得分:1)
使用案例表达式获取订单密钥:
select *
from book_history
order by
case status
when 1 then 100 -- any small value would do here
when 0 then 200 -- any medium value would do here
when 2 then 300 -- any big value would do here
end;
答案 4 :(得分:0)
UNION
的另一种选择:
select * from book_history where status = 1
UNION
select * from book_history where status = 0
UNION
select * from book_history where status = 2