2012-07-09 69 views
0

存在混淆我无法获得可行的查询,我可以有多个必需的记录和单个“一个”存在。我正在使用Hibernate,hql,sql显然不会有根本的不同,但这意味着我不能使用完全连接(因此很尴尬)。SQL - 混合与'和'和'或'

我基本上要,但不能因为我不知道如何搭配内进出存在电话:

select 1 
from application 
where 
application = ? and 
exists (select 1 from x where ...) and 
exists (select 1 from y where ...) and 
exists ((exists (select 1 from Document where ...)) 
or 
(exists (select 1 from QA answer where ...))) 

所以我需要X & Y,但无论是文档或QA是可以接受的,只要1存在。

回答

2

像这样的东西?

select 1 
from application 
where 
application = ? and 
exists (select 1 from x where ...) and 
exists (select 1 from y where ...) and 
(
    exists (select 1 from Document where ...) 
    or 
    exists (select 1 from QA answer where ...) 
); 
+0

这似乎工作,至少在SQL中。 谢谢! 我以为我曾尝试过,但过于复杂的查询肯定意味着不正确的括号。 (我曾建议分解查询,但被击落) – 2012-07-09 11:42:04