2017-03-17 69 views
1

我们可以通过在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 (...)过滤排序的条目排序?

回答

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); 
+0

混账!我今天学到了一件新事物,**非常感谢你!这正在困扰我很多年,只有现在花时间去实际要求它:) – balexandre

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