0

Hi I need to do the following using Criteria

Select * from product pd, (Select productID pd1 from ... where ...) tpd1, 
(Select productID pd2 from ... where ...) tpd2
where pd.productID = tpd1.pd1 and pd.productID = tpd1.pd2

May I know if it is possible?

The original SQL was using IN conditions

Select * from product pd where productID in (Select productID pd1 from ... where ...) and 
productID in (Select productID pd2 from ... where ...) 

but it takes too long to get the result, using the join SQL statement I was able to obtain my result faster.

any help?

4

1 回答 1

0

鉴于您使用的是 Hibernate,您可能必须执行以下操作,如果预期匹配的数量相对较低,这应该可以正常工作:

select *
from   product pd
where  pd.productID in
   (select productID
    from   product pd2
    join   tpd1 on (pd2.productID = tpd1.pd1)
    join   tpd2 on (pd2.productID = tpd2.pd2)
    where  tpd1....
    and    tpd2....
   );

我假设product.productID.

或者,您可以尝试 EXISTS 公式,它可能会或可能不会比您的原始查询更好:

select *
from   product pd
where  EXISTS
   (select null from tpd1 where pd.productID = tpd1.pd1 and ...)
and    EXISTS
   (select null from tpd2 where pd.productID = tpd2.pd2 and ...)
;
于 2012-08-30T03:24:49.693 回答