試試這個:
select * from QuotesTable where quotesid in (103,7,16,50,41,80,67,64)
order by case quotesid when 103 then 1
when 7 then 2
when 16 then 3
when 50 then 4
when 41 then 5
when 80 then 6
when 67 then 7
when 64 then 8
end
如果這些值增長,那麼你可以在數據庫中創建一個表:
create table QuotesOrderingTable(quotesid int, orderid int)
go
用適當的值填充它:
insert into QuotesOrderingTable values
(103, 1),
(7, 2),
(16, 3),
(50, 4),
(41, 5),
(80, 6),
(67, 7),
(64, 8),
(..., 9),
(..., 10),
...
,然後用它通過:
select qt.* from QuotesTable qt
join QuotesOrderingTable qot on qt.quotesid = qot.quotesid
where qt.quotesid in (103,7,16,50,41,80,67,64)
order by qot.orderid
你的桌子上是否有ID列反映了順序?或者可以考慮在表格中添加一個訂單列以獲取正確的訂單?什麼決定了隨機查找的順序? – FuzzyJulz
id列是quotesid,它是主鍵。 – Venkat
我不認爲你明白我的意思,你說你需要按順序'103,7,16,50,41,80,67,64'這是什麼意思?通常你會在你的數據庫中有一個訂單欄,這樣你就可以在這種情況下簡單地訂購。 – FuzzyJulz