首页 > 解决方案 > 连接条件的 case 语句

问题描述

嗨,我想在 where 条件或一些类似的逻辑中使用 case 语句。我想在 tmp_collaboration 表中没有行时忽略 where 条件,并在表中有一些行时使用条件。

Select clbid from tmp_collaboration;

select customer_id, product_id ,clbid 
from customers c
    where hdr_id = 10
          and clbid in (select clbid from tmp_collaboration)
          and status = 'y';

标签: sqlpostgresql

解决方案


这是你想要的吗?

select customer_id, product_id ,clbid 
from customers c
where hdr_id = 10 and status = 'y' and
      (clbid in (select clbid from tmp_collaboration) or
       not exists (select 1 from tmp_collaboration)
      );

推荐阅读