我正在尝试计算表中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
发布于 2020-08-07 19:04:05
如果您只需要一行
如果您只需要一行,那么应该将该谓词添加到您的查询中,即
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的Result,或者在Result上使用Result.intoGroups(IDEA.GP_STATUS).get("x")或任何其他方法来执行类似的操作。
https://stackoverflow.com/questions/63297901
复制相似问题