[英]Do left outer join only if condition satisfies in JPA
I am new to JPA and trying to do a left outer join, only if a condition is satisfied in the native JPA query.我是 JPA 的新手,并且仅在本机 JPA 查询中满足条件时才尝试进行左外连接。
Native query is as shown below.本机查询如下所示。 Here I would like to do A left outer join B
with the where condition, only if a given flag toJoin
is true in application:在这里,我想使用 where 条件执行A left outer join B
,仅当应用程序中给定的toJoin
标志为 true 时:
select a.id, a.name from A left outer join B on A.id = B.aid where b.name="some_name"
I am not sure, how will I accommodate the toJoin
application flag in the above native JPA query in my JPA repository.我不确定,我将如何在我的 JPA 存储库中的上述本机 JPA 查询中容纳toJoin
应用程序标志。
EDIT编辑
I was just trying to achieve the same in mysql with the help of CASE WHEN
and sample condition as follows:我只是试图在CASE WHEN
和示例条件的帮助下在 mysql 中实现相同的目标,如下所示:
select a.id from A a, case when 1=2 then left outer join B b ON a.id = b.aid end limit 2;
But I am getting syntaxt error.但我收到语法错误。
Your question remains vague, however I suspect that your effort:您的问题仍然含糊不清,但我怀疑您的努力:
select a.id
from A a, case when 1=2 then left outer join B b on a.id = b.aid end
limit 2
Is meant to be:是指:
select a.id, b.someColumn
from A a
left join B b on a.id = b.aid and 1=2
limit 2
Where 1=2
is actually some more complicated expression.其中1=2
实际上是一些更复杂的表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.