考虑下表
ColID Value
A 100
B 200
C 300
D 400
E 500
以相同顺序检索colID'C','A','D'的值的查询是什么?
答案 0 :(得分:3)
对于特定的顺序,您必须执行以下操作:
SELECT * FROM MyTable WHERE ColID IN ('C', 'A', 'D')
ORDER BY
CASE ColID
WHEN 'C' THEN 0
WHEN 'A' THEN 1
ELSE 2
END
答案 1 :(得分:3)
如何使用CASE?
SELECT [Value]
FROM testTable
WHERE colID IN ('C','A','D')
ORDER BY (CASE colID WHEN 'C' THEN 1 WHEN 'A' THEN 2 WHEN 'D' THEN 3 END);
答案 2 :(得分:1)
Select * from Tablex Where ColId in ('C', 'A', 'D') order by ColId -- This will yield 'A', 'C', 'D' order
如果您需要以'C'''''D'顺序返回,则需要另外一列如下:
ColID Value Ordinal
A 100 2
B 200 5
C 300 1
D 400 3
E 500 4
然后做:
Select * from Tablex Where ColId in ('C', 'A', 'D') order by Ordinal
答案 3 :(得分:0)
您可以这样做:
select *
from your table
Where ColId in ('C', 'A', 'D')
对于该特定订单,您无法按ColId或Value排序。订单不会按顺序提供给您。