我在使用hibernate原生查询的Java7应用程序中使用Postgres 12.6。
我正在尝试构造一个SQL语句,该语句读取表journalheader的结果,其中它接收一个参数。参数值可以是‘匹配的’或‘不匹配的’。
以下解决方案将是不够的,因为它不适合null值:
select * from journalheader where matched_status = ?1;该表确实有一些带有matched_status列的null行。我希望这些行成为‘无与伦比’过滤器的一部分。
即
如果参数是“匹配”的,则只返回'Matched'.
(?1)的行,然后只返回matched_status =“不匹配”或null的行。问题
如何也检查null值?
可能的解决方案
我想我可能需要在参数值上添加一个检查,如果它是,‘不匹配’,在语句中包括or matched_status is null。我只是不确定如何检查sql语句中参数的值。
select * from journalheader where matched_status = ?1 or <if ?1 equals 'Unmatched'> matched_status is null;发布于 2022-05-20 08:26:54
您可以尝试使用COALESCE
select *
from journalheader
where matched_status = COALESCE(?1,'Unmatched') 或CASE WHEN
select *
from journalheader
where matched_status =CASE WHEN ?1 is null THEN 'Unmatched' ELSE ?1 END https://stackoverflow.com/questions/72315497
复制相似问题