我写了这个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查询? 谢谢,
答案 0 :(得分: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
谓词的子查询中的表别名,以简化操作。当然,您也可以继续对所有这些表进行别名。
答案 1 :(得分: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);
这项工作很好 谢谢