如何在jOOQ中编写计数查询 [英] How to write Count Query In jOOQ

查看:380
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆