我试图计算表中gp_status字段的不同可能性。我有一个有效的sql查询,只是无法弄清楚如何将其转录为Jooq。
select i.gp_status, COUNT(i.gp_status)
from ideas_service.idea i
group by i.gp_status
到目前为止,在JOOQ中我有这个
var result = dsl.select(IDEA.GP_STATUS,count(),count(IDEA.GP_STATUS))
.from(IDEA)
.groupBy(IDEA.GP_STATUS)
.fetch();
看起来字段正确恢复了,但是我不知道如何提取它们。 我确实知道gp_status可能是什么。
所以我需要以某种方式获取行where gp_status = x
答案 0 :(得分:0)
如果只需要一行,则应将该谓词添加到查询中,即
var result = dsl.select(IDEA.GP_STATUS, count())
.from(IDEA)
.where(IDEA.GP_STATUS.eq("x"))
.groupBy(IDEA.GP_STATUS)
.fetchOne();
此时,您不再需要GROUP BY
子句:
var result = dsl.select(count())
.from(IDEA)
.where(IDEA.GP_STATUS.eq("x"))
.fetchOne();
请注意,在两种情况下,我都使用过ResultQuery.fetchOne()
,它会产生一个Record
,您可以通过多种方式从中提取值,例如
// Repeat the column expression from the SELECT clause
int count1 = result.get(count());
// Access columns by index, and redundantly supply the type again
// Assuming the second query was executed
int count2 = result.get(0, int.class);
还有更多方法。
如果您需要整个结果集,但在特定情况下,只想提取一行,则可以迭代扩展List
的{{3}}或使用Result.intoGroups(IDEA.GP_STATUS).get("x")
或Result
上的任何其他方法来做类似的事情。