如何在jOOQ中编写计数查询 [英] How to write Count Query In jOOQ
问题描述
我现在有这个,正在将Pure SQL转换为jOOQ
I am converting Pure SQL to jOOQ now I have this
("SELECT Count(*) Count From Table ");
我必须在jOOQ中编写此代码,我们如何编写它?
I have to write this in jOOQ how can we write it?
selectQueryRecord.addSelect(Here Count Function );
selectQueryRecord.addFrom(Table);
推荐答案
The most straight-forward way to implement what you're requesting is this, by using selectCount()
:
int count =
DSL.using(configuration)
.selectCount()
.from(Table)
.fetchOne(0, int.class);
或者,您可以明确表示 count()
函数:
Alternatively, you can explicitly express the count()
function:
int count =
DSL.using(configuration)
.select(DSL.count())
.from(Table)
.fetchOne(0, int.class);
还有另一种获取任何任意select
表达式的count(*)
,可以帮助您避免在上述fetchOne()
方法中指定结果列索引和类型.这使用 fetchCount()
:
There's another alternative for fetching a count(*)
of any arbitrary select
expression, which helps you avoid specifying the result column index and type in the above fetchOne()
method. This uses fetchCount()
:
int count =
DSL.using(configuration)
.fetchCount(DSL.selectFrom(Table));
但是请注意,这会呈现如下嵌套选择:
Beware, though, that this renders a nested select like this:
SELECT COUNT(*) FROM (SELECT a, b, ... FROM Table)
这篇关于如何在jOOQ中编写计数查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!