如何解决在SQL查询中添加if子句
我有一个查询
sql_query := ' Select A,B,C from table where A = 'DG54FDG45SD' and B = 'FS487' ;
,我想为它添加一个if子句:
sql_query := ' Select A,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'));
谢谢!
解决方法
您不必在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
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。