2017-03-17 89 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