0

我需要一个 WHERE 子句来仅评估某些语句。就像是:

WHERE field_a = field_b AND
   (CASE WHEN <PARAM>type</PARAM> = 5
      THEN {field_c = 1 OR field_c = 2}
      WHEN <PARAM>type</PARAM> = 6
      THEN {field_c = 3 OR field_c = 4}
      ELSE field_c = <PARAM>type</PARAM>

这样当 Param 类型 = 5 时,它只检查 field_c = 1 还是 2。有什么想法吗?

4

5 回答 5

3
WHERE 
((field_a = field_b)) AND
(((<PARAM>type</PARAM> = 5) AND (field_c IN (1, 2))) OR
((<PARAM>type</PARAM> = 6) AND (field_c IN (3, 4))) OR
((<PARAM>type</PARAM> NOT IN (5, 6) AND <PARAM>type</PARAM> = field_c)))
于 2010-04-09T12:57:13.877 回答
0
WHERE field_a = field_b AND
      CASE WHEN <PARAM>type</PARAM> = 5 THEN
                CASE WHEN field_c = 1 OR field_c = 2 THEN 1 ELSE 0 END
           WHEN <PARAM>type</PARAM> = 6 THEN
                CASE WHEN field_c = 3 OR field_c = 4 THEN 1 ELSE 0 END
           ELSE
                CASE WHEN field_c = <PARAM>type</PARAM> THEN 1 ELSE 0 END
      END = 1
于 2010-04-09T14:22:33.930 回答
0
WHERE field_a = field_b
AND (
    (type = 5 AND (field_c = 1 OR field_c = 2))
    OR (type = 6 AND (field_c = 3 OR field_c = 4))
    OR field_c = type
)

这就是你所追求的吗?

于 2010-04-09T12:56:34.877 回答
0

这会做吗?

Where field_a = field_b AND
(
   (<PARAM>type</PARAM> = 5 AND field_c IN (1,2))
   OR (<PARAM>type</PARAM> = 6 AND field_c IN (1,2))
   OR (field_c = <PARAM>type</PARAM>)
)
于 2010-04-09T13:00:46.897 回答
0

这就是我想出的,在从帖子中得到一些启发后,我意识到我不需要这样的条件代码。此外,Param 类型具有接受多个值的能力,并以逗号分隔输出它们,这很有帮助。

WHERE field_a = field_b AND
   (field_c in (<PARAM>type</PARAM>))

谢谢您的帮助!

于 2010-04-09T13:20:43.173 回答