2

我写了这个 mysql 查询并尝试将其转换为 JOOQ 查询但没有成功,这是 mysql 查询

SELECT `P`.`phone_number`, `A`.`emp_no`,   
SUM(CASE WHEN VCG.vid IN (
SELECT gv.vid FROM `grvas` gv JOIN gprs gr ON gr.id=gv.grid 
WHERE gr.id=G.id AND gv.stdate < '2011-08-15'  AND gv.enddate > '2011-09-14') 
THEN VCG.amount END) AS allow,
... etc...

如何将此查询转换为 JOOQ 查询?
谢谢,

4

2 回答 2

4

这:

SUM(
  CASE 
  WHEN VCG.vid IN (
    SELECT gv.vid 
    FROM `grvas` gv 
      JOIN gprs gr ON gr.id=gv.grid 
    WHERE gr.id=G.id 
      AND gv.stdate < '2011-08-15'  
      AND gv.enddate > '2011-09-14'
  ) 
  THEN VCG.amount 
  END
) AS allow

会翻译成这样的:

import static org.jooq.impl.DSL.*;

// And then
sum(
  decode()
  .when(VCG.VID.in(
      select(GRVAS.VID)
      .from(GRVAS)
        .join(GPRS).on(GPRS.ID.eq(GRVAS.GRID))
      .where(GPRS.ID.eq(G.ID))
      .and(GRVAS.STDATE.lt(Date.valueOf("2011-08-15")))
      .and(GRVAS.ENDDATE.gt(Date.valueOf("2011-09-14")))
  )), VCG.AMOUNT)
).as("allow")

上面的例子在谓词的子查询中省略了表别名IN,以简化事情。当然,您也可以继续为所有这些表设置别名。

于 2013-01-31T11:15:37.857 回答
0

我找不到将上述 sql 转换为 JOOQ 的正确方法,但我使用了

String sql="SELECT `P`.`phone_number`, `A`.`emp_no`,   
             SUM(CASE WHEN VCG.vid IN (
             SELECT gv.vid FROM `grvas` gv JOIN gprs gr ON gr.id=gv.grid 
             WHERE gr.id=G.id AND gv.stdate < '2011-08-15'  AND gv.enddate > '2011-09-14') 
             THEN VCG.amount END) AS allow,
             ... etc... " ;
Result<Record> res = f.fetch(sql);  

这工作很好谢谢

于 2013-01-31T09:59:17.580 回答