如何在带有IS NULL的WHERE子句中使用CASE语句?

cod*_*dea 5 sql oracle oracle11g

这是我的查询,它们不起作用,但我想做这样的事情:

SELECT a_field FROM a_table
WHERE
... 
AND
CASE
WHEN a_value_from_another_query IS NULL THEN a_second_field IS NULL
ELSE a_second_field = a_value_from_another_query
END
Run Code Online (Sandbox Code Playgroud)

要么

SELECT a_field FROM a_table
WHERE
... 
AND
CASE a_value_from_another_query
WHEN NULL THEN a_second_field IS NULL
ELSE a_second_field = a_value_from_another_query
END
Run Code Online (Sandbox Code Playgroud)

要么

SELECT a_field FROM a_table
WHERE
... 
AND
CASE NVL(a_value_from_another_query, 'x')
WHEN 'x' THEN a_second_field IS NULL
ELSE a_second_field = a_value_from_another_query
END
Run Code Online (Sandbox Code Playgroud)

什么时候a_value_from_another_query IS NULL,我想添加a_second_field IS NULL到我的WHERE子句,何时a_value_from_another_query IS NOT NULL,我想添加a_second_field = a_value_from_another_query到我的WHERE子句.我怎样才能做到这一点?

sta*_*san 10

听起来你只是从工具箱中选择了错误的工具.

除非我非常误解你,否则:

WHERE
    (a_value_from_another_query IS NULL AND a_second_field IS NULL)
  OR
    (a_value_from_another_query IS NOT NULL AND a_second_field = a_value_from_another_query)
Run Code Online (Sandbox Code Playgroud)

......应该是你想要的.