我们如何“按顺序”过滤订单

时间:2017-03-17 10:02:59

标签: sql mariadb

我们可以通过我们在in子句中注入的ID对SQL查询进行排序的技巧是什么?

像:

select oh.orderID, oh.orderType, oh.state, oh.orderDateTime
from orderHeaders oh 
where oh.orderID in (
47185154,
47185121,
47184971,
47863101)

我的orderID字段就像:

47184971...
47863101...
47185121...
47185154...

如何通过WHERE IN (...)过滤器中的条目排序获得排序结果?

2 个答案:

答案 0 :(得分:1)

您可以使用field()

select oh.orderID, oh.orderType, oh.state, oh.orderDateTime
from orderHeaders oh 
where oh.orderID in (47185154, 47185121, 47184971, 47863101)
order by field(oh.orderID, 47185154, 47185121, 47184971, 47863101);

答案 1 :(得分:0)

您可以按顺序

中的顺序定义它们
ORDER BY
CASE oh.OrderID 
WHEN '47185154' THEN 1
WHEN '47185121' THEN 2
WHEN '47184971' THEN 3
WHEN '47863101' THEN 4
ELSE 5
END