我们如何"order by"过滤顺序
How do we "order by" filtering order
为了让 SQL 查询按我们在 in
子句中注入的 ID 排序,我们可以使用什么技巧?
喜欢:
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 (...)
过滤器中的条目排序的结果?
您可以按子句的顺序定义它们
ORDER BY
CASE oh.OrderID
WHEN '47185154' THEN 1
WHEN '47185121' THEN 2
WHEN '47184971' THEN 3
WHEN '47863101' THEN 4
ELSE 5
END
您可以使用 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);
为了让 SQL 查询按我们在 in
子句中注入的 ID 排序,我们可以使用什么技巧?
喜欢:
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 (...)
过滤器中的条目排序的结果?
您可以按子句的顺序定义它们
ORDER BY
CASE oh.OrderID
WHEN '47185154' THEN 1
WHEN '47185121' THEN 2
WHEN '47184971' THEN 3
WHEN '47863101' THEN 4
ELSE 5
END
您可以使用 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);