如何在Oracle的where子句中使用IF THEN ELSE语句?

时间:2016-09-30 09:41:07

标签: sql oracle

我很有意思,我如何在Oracle中的where子句中使用if-then-else语句或任何控制结构。我想用作:

  • pcustomer_id IS NULL然后WHERE c.customer_id IS NULL
  • 以及pcustomer_id IS NOT NULL然后c.customer_id = pcustomer_id

我该怎么做?

SELECT *
  FROM customer_inf c
 WHERE 
IF
 pcustomer_id IS NULL THEN ( c.customer_id IS NULL ) 
    ELSE (c.customer_id = pcustomer_id) END IF;

4 个答案:

答案 0 :(得分:4)

你需要捆绑它们

where (
(pcustomer_id is null 
    and c.customer_id is null)
or 
(pcustomer_id is not null 
    and c.customer_id = pcustomer_id)
)

答案 1 :(得分:1)

使用布尔表达式:

SELECT *
FROM   customer_inf c
WHERE  ( pcustomer_id IS NULL AND c.customer_id IS NULL )
OR     ( pcustomer_id IS NOT NULL AND cc.customer_id = pcustomer_id);

答案 2 :(得分:0)

如果我理解正确你只想在c.customer_id = pcustomer_id(包括null equity)时返回所有值。所以:

select * from customer_inf c 
where nvl(pcustomer_id,'999999999999') = nvl(c.customer_id, '999999999999');

您只需选择实际数据中不会出现的数字。

答案 3 :(得分:0)

WHERE NVL(pcustomer_id,c.customer_id) IS NULL OR NVL2(pcustomer_id,c.customer_id,pcustomer_id)=pcustomer_id

说明: 在此,在第一个NVL中,如果pcustomer_id为null,则传递c.customer_id并将检查Null 在第二个NVL2 1中,如果pcustomer_id不为null,则c.customer_id = pcustomer_id