如何通过命令放入"IN"子句中的条件从数据库中获取结果?

Sha*_*shi 2 sql database sql-server-2005

抱歉英文不好.

bfproduct是一个表,productid是此表中的主键,productname是此表中定义的另一个字段.

当我执行此查询时,select * from bfproduct where productid in (23,5,54,3132,32).结果如下:

 

productid | productname

5         15 Park Avenue
23        Good Boy Bad Boy
32        dsf sf gfdsf dsf d  
54       dsdsfsa ffs ff sfsf 
3132        Just Books - On The Failure of Legal System

有没有什么方法可以通过"IN"条款中提供的productid按顺序获得结果集


productid | productname
23        Good Boy Bad Boy
5         15 Park Avenue
54       dsdsfsa ffs ff sfsf 
3132        Just Books - On The Failure of Legal System
32        dsf sf gfdsf dsf d  

请帮忙...

Mar*_*ers 9

这是一种方法:

SELECT *
FROM bfproduct
WHERE productid
IN (23,5,54,3132,32)
ORDER BY
   CASE productid
      WHEN   23 THEN 0
      WHEN    5 THEN 1
      WHEN   54 THEN 2
      WHEN 3132 THEN 3
      WHEN   32 THEN 4
   END
Run Code Online (Sandbox Code Playgroud)