Java 如何在 jOOQ 中编写计数查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19656991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write Count Query In jOOQ
提问by Subodh Joshi
I am converting Pure SQL to jOOQ now I have this
我正在将 Pure SQL 转换为 jOOQ 现在我有了这个
("SELECT Count(*) Count From Table ");
I have to write this in jOOQ how can we write it?
我必须在jOOQ中写这个我们怎么写?
selectQueryRecord.addSelect(Here Count Function );
selectQueryRecord.addFrom(Table);
采纳答案by Lukas Eder
The most straight-forward way to implement what you're requesting is this, by using selectCount()
:
实现您所要求的最直接的方法是使用以下方法selectCount()
:
int count =
DSL.using(configuration)
.selectCount()
.from(Table)
.fetchOne(0, int.class);
Alternatively, you can explicitly express the count()
function:
或者,您可以明确表达该count()
函数:
int count =
DSL.using(configuration)
.select(DSL.count())
.from(Table)
.fetchOne(0, int.class);
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()
:
还有另一种方法可以获取count(*)
任意select
表达式的 a,它可以帮助您避免在上述fetchOne()
方法中指定结果列索引和类型。这使用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)
回答by Subodh Joshi
Here is the solution We have to use like this
这是我们必须像这样使用的解决方案
selectQueryRecord.fetchCount();
回答by psisodia
NOTE:
笔记:
Deprecated. - 3.5.0 - [#3356] - This method is being removed as it is confusingly different from all the other types of ResultQuery.fetch() methods, in that it modifies the original Select statement by wrapping it. In particular, this method can be easily confused with ResultQuery.fetch(Field), or more concretely fetch(count()), which has an entirely different semantics. Use DSLContext.fetchCount(Select) instead. Execute this query in the context of its attached executor and return a COUNT(*) value.
已弃用。- 3.5.0 - [#3356] - 此方法被删除,因为它与所有其他类型的 ResultQuery.fetch() 方法不同,因为它通过包装它来修改原始 Select 语句。特别是,这个方法很容易与 ResultQuery.fetch(Field) 混淆,或者更具体地说 fetch(count()) ,它们具有完全不同的语义。使用 DSLContext.fetchCount(Select) 代替。在其附加执行程序的上下文中执行此查询并返回 COUNT(*) 值。
I think the proper way to write get the count would be like this:
我认为写 get 计数的正确方法是这样的:
SelectQuery<Record> selectQueryCount = transaction.selectQuery();
selectQueryCount.addFrom(Table);
Result<Record> resultObject = selectQueryRecord.fetchCount();
回答by Kevin Davin
I use the following syntax for this:
我为此使用以下语法:
import org.jooq.impl.DSL.count
...
int count =
DSL.using(configuration)
.selectCount()
.from(Table)
.fetchOne(count());
This is less verbose and simpler.
这不那么冗长和简单。
Lukas's answer dates from 2013, maybe this solution did not exist at the time.
Lukas 的回答可以追溯到 2013 年,也许当时这个解决方案并不存在。
回答by Jesús Sánchez
I used this:
我用过这个:
Integer count = DSL.selectCount().from(Table).where(Table.FIELD.eq(value)).fetchOneInto(Integer.class);
Integer count = DSL.selectCount().from(Table).where(Table.FIELD.eq(value)).fetchOneInto(Integer.class);