如何在Jooq中使用自我加入?

时间:2013-12-09 10:13:36

标签: java sql jooq

我们可以在JOOQ中使用SelfJoin吗?

Select Count(1) CountPayments 
From PaymentDetail APD1, PaymentDetail APD2, Payment AP 
where APD1.PaymentNumber =123 
  and APD1.BillNumber > 0 
  and APD2.BillNumber = APD1.BillNumber 
  and APD2.PaymentNumber <> APD1.PaymentNumber 
  and AP.PaymentNumber = APD2.PaymentNumber

如果是,我们如何在上方查询中使用它?

1 个答案:

答案 0 :(得分:1)

The manual's section about aliasing tables可能会给你一些线索。

基本上,只需为表分配别名:

PaymentDetail APD1 = PaymentDetail.as("APD1");
PaymentDetail APD2 = PaymentDetail.as("APD2");
Payment AP = Payment.as("AP");

DSL.using(configuration)
   .select(count(1).as("CountPayments"))
   .from(APD1, APD2, AP)
   .where(APD1.PaymentNumber.eq(123))
   .and(APD1.BillNumber.gt(0))
   .and(APD2.BillNumber.eq(APD1.BillNumber))
   .and(APD2.PaymentNumber.ne(APD1.PaymentNumber))
   .and(AP.PaymentNumber.eq(APD2.PaymentNumber))