首页 > 解决方案 > 当列值为空时,不要在 where 条件中附加“and”子句

问题描述

我有一个查询,我不想在列值为空时在 where 条件中附加“and”子句,例如

select * from x where x.abc = 'ACTIVE' and x.start_date < sysdate and x.end_date > sysdate

因此,我希望 x.end_date 仅在其值不为 null 时才适用。没有任何存储过程,只是一个简单的查询。

我还想使用标准构建器和谓词将此查询转换为 spring-data jpa 规范。

标签: sqloraclespring-data-jpa

解决方案


您可以使用or

select *
from x
where x.abc = 'ACTIVE' and
      x.start_date < sysdate and
      ( x.end_date > sysdate or x.end_date is null );

推荐阅读