如何解决Oracle问题时的情况?

时间:2014-04-30 19:11:27

标签: sql oracle

我有一个如下所示的查询:

SELECT * 
FROM   tbl a
   WHERE   A.DEVICE_CAT IN
       (CASE :device1
            WHEN 'A' THEN 'a1'
            WHEN 'B' THEN 'b1'
            WHEN 'C' THEN 'c1'
            WHEN 'D' THEN 'd1'
            WHEN 'E' THEN 'e1'
            WHEN 'F' THEN '(f1, f2)'
            ELSE ' '
         END)

对于:device1='F'的情况,我想选择device_cat in ('f1','f2')的结果。怎么写呢?我目前的查询不正确。

1 个答案:

答案 0 :(得分:3)

我不是case条款中where的粉丝。我觉得这个逻辑难以理解。我会打破逻辑:

WHERE (:device1 = 'A' and A.DEVICE_CAT = 'a1') OR
      (:device1 = 'B' and A.DEVICE_CAT = 'b1') OR
      (:device1 = 'C' and A.DEVICE_CAT = 'c1') OR
      (:device1 = 'D' and A.DEVICE_CAT = 'd1') OR
      (:device1 = 'E' and A.DEVICE_CAT = 'e1') OR
      (:device1 = 'F' and A.DEVICE_CAT IN ('f1', 'f2'));

此外,您无法在case列表中嵌套in