【发布时间】:2014-11-15 18:08:41
【问题描述】:
我有这样的 SQL (postgresql) 表达式:
select * from table1 t1 left join table2 t2 on t1.id = t2.id where
t1.name = nameVariable and t2.field = fieldVariable;
如果 nameVariable 为 null 或为空,是否有可能不执行“where t1.name = nameVariable " 吗?
编辑 我还需要准确说明我正在使用 JPA 和休眠,所以查询看起来像这样:
@Query(value = " select * from table1 t1 left join table2 t2 on t1.id = t2.id where
t1.name = :name and t2.field = :fieldName;", nativeQuery=true)
public List<Test> getTest(@Param("name") String name, @Param("fieldName") String fieldName);
【问题讨论】:
-
是的。有可能的。顺便说一句:您的左连接通过在 where 子句
t2.field = fieldVariable中引用它来简化为普通连接 -
您能告诉我如何实现这一目标吗?谢谢
标签: postgresql variables null where