在SQL查询中添加if子句

时间:2020-09-01 08:06:48

标签: sql oracle plsql

我有一个查询

sql_query := ' Select A,B,C from table where A = 'DG54FDG45SD' and B = 'FS487' ;

,我想为它添加一个if子句:

sql_query := ' Select A,B,C,D from table where A = 'DG54FDG45SD' and B = 'FS487' and  (if C  = 'AR' then AND  D in (select uid from table2 where id = 'DGF'));

谢谢!

1 个答案:

答案 0 :(得分:0)

您不必在if子句中使用where来执行此操作-仅仅是因为没有必要。您可以使用基本的布尔运算得出相同的逻辑:

where A = 'DG54FDG45SD' and
      B = 'FS487' and
      (D in (select uid from table2 where id = 'DGF') or
       C <> 'AR'
      )

如果您需要考虑NULL

where A = 'DG54FDG45SD' and
      B = 'FS487' and
      (D in (select uid from table2 where id = 'DGF') or
       C <> 'AR' or C is null
      )
相关问题