此问题源自another question
请参阅上下文。
基本上我想使用下面的SQL查询来检索记录列表:
Select * From [TableA] Where [A_Design] In ("A", "D", "C" , "B")
据我所知," In"条款不保证检索到的记录将遵循与(" A"," D"," C"," B")相同的顺序
我的问题是我们如何实现这一目标?或者列表是否是动态生成的长列表?
感谢Gordon Linoff,问题解决了!以下是从他的例子中修改的工作解决方案:
select instr(",A,D,C,B,", "," & [A_Design] & ",")
from [TableA]
where [A_Design] In ("A", "D", "C" , "B")
order by 1;
非常感谢Luis Siquot,你的解决方案也有效,但与Instr()方法相比,代码更长一些。
答案 0 :(得分:4)
您可以使用一堆嵌套的iif()
语句。另一种方法是使用instr()
:
select *
from TableA
where [A_Design] In ("A", "D", "C" , "B")
order by instr("ADCB", A_Design);
注意:这适用于单个字符代码。对于较长的代码,您应该使用分隔符:
select *
from TableA
where [A_Design] In ("A", "D", "C" , "B")
order by instr(",A,D,C,B,", "," & A_Design & ",");
答案 1 :(得分:3)
在您的order by子句中使用swich,如下所示
Select * From [TableA] Where [A_Design] In ("A", "D", "C" , "B")
order by switch(
A_Design ="A", 1,
A_Design ="D", 2,
A_Design ="C", 3,
A_Design ="B", 4
)
Swich相当于标准case when