如何简化下一个where子句?:
where
e.withdrawalMonth is null
and ((:debt = 1 and not exists
(from
PaidConcept cc
join cc.invoice i
where
cc.enrollment = e
and cc.month = m
and cc.concept = co
and i.status = 'PAID'
))
or(:debt = 0 and exists
(from
PaidConcept cc
join cc.invoice i
where
cc.enrollment = e
and cc.month = m
and cc.concept = co
and i.status = 'PAID'
))
)
有两个相等的子查询,但第一个是not exists
,第二个是exists
我希望在debt
参数为1时首先运行,在debt
参数为0时最后运行
怎么可以做得更干?
答案 0 :(得分:0)
String subquery = (from
PaidConcept cc
join cc.invoice i
where
cc.enrollment = e
and cc.month = m
and cc.concept = co
and i.status = 'PAID'
))
String query = "where e.withdrawalMonth is null
+ " and ((:debt = 1 and not exists "
+ subquery
+ " or(:debt = 0 and exists "
+ subquery + ")"