我有以下查询。我需要替换IN查询
SELECT * FROM TABLE1
WHERE
FIELD2 LIKE '%SEARCH_DATA%'
AND (CASE WHEN CONDITION1=TRUE THEN FIELD1
ELSE 'TEXT1'
END IN ('TEXT1','TEXT2'))
请您告诉我如何用相同的条件替换IN。
答案 0 :(得分:1)
尝试这样的事情,
SELECT *
FROM TABLE1
WHERE CONDITION1 <> TRUE -- CONDITION1 = FALSE
OR (FIELD1 = 'TEXT1' OR FIELD1 = 'TEXT2')
答案 1 :(得分:1)
这转换为:
SELECT * FROM TABLE1
WHERE FIELD2 LIKE '%SEARCH_DATA%'
AND
(
(CONDITION1 = TRUE AND FIELD1 IN ('TEXT1','TEXT2'))
OR CONDITION1 = FALSE
OR CONDITION1 IS NULL
)
如果CONDITION只能为TRUE或FALSE而不是NULL,那么我们可以将其简化为:
SELECT * FROM TABLE1
WHERE FIELD2 LIKE '%SEARCH_DATA%' AND (CONDITION1 = FALSE OR FIELD1 IN ('TEXT1','TEXT2'))
(否则我们必须将CONDITION1 = FALSE
替换为CONDITION1 = FALSE OR CONDITION1 IS NULL
)。
没有IN
(虽然我更喜欢IN
):
SELECT * FROM TABLE1
WHERE FIELD2 LIKE '%SEARCH_DATA%'
AND (CONDITION1 = FALSE OR FIELD1 = 'TEXT1' OR FIELD1 = 'TEXT2')