如何从Jooq中的其他自定义(concat,sum,count)列获取数据库中的所有结果列 [英] How to get all the result columns from database with other custom(concat, sum, count) columns in Jooq

查看:1190
本文介绍了如何从Jooq中的其他自定义(concat,sum,count)列获取数据库中的所有结果列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含6列的Table1表。

I have a table Table1 with 6 columns.

这是我需要映射的sql语句。

Here is the sql statement that i need to map.

Select *,count(ID) as IdCount from Table1;

现在,sql查询结果将是7列(6个Table1列和1个IdCount列)。但是当我使用此查询在Jooq中实现相同时,它只获得一个列IDCount。

Now, the sql query result will be 7 columns ( 6 Table1 columns and 1 IdCount column). But when i implement the same in Jooq with this query, it only gets a single column "IDCount".

SelectQuery q = factory.selectQuery();
        q.addSelect(Table1.ID.count().as("IdCount"));
        q.addFrom(Table1.TABLE1);

现在,结果记录集只有一列IdCount,而我需要的是所有列还有一栏IdCount。我也想在Jooq中使用7列。

Now, the resultant recordset have only a single column "IdCount" while what i need is all the columns and one additional column "IdCount". I want 7 columns in Jooq too.

推荐答案

选项1 (使用星号):

* (星号,星号)运算符已通过 DSL.asterisk() (不合格的星号)或通过 Table.asterisk() (合格的星号)。它可以像预测的任何其他列一样使用。

The * (asterisk, star) operator has been added to jOOQ 3.11 through DSL.asterisk() (unqualified asterisk) or through Table.asterisk() (qualified asterisk). It can be used like any other column being projected.

在jOOQ 3.11之前,还有以下其他选项:

Prior to jOOQ 3.11, there were the following other options as well:

选项2 (使用DSL语法):

Option 2 (with the DSL syntax):

List<Field<?>> fields = new ArrayList<Field<?>>();
fields.addAll(Arrays.asList(Table1.TABLE1.fields()));
fields.add(Table1.ID.count().as("IdCount"));

Select<?> select = ctx.select(fields).from(Table1.TABLE1);

选项3 (使用常规语法) :

Option 3 (with the "regular" syntax, which you used):

SelectQuery q = factory.selectQuery();
q.addSelect(Table1.TABLE1.fields());
q.addSelect(Table1.ID.count().as("IdCount"));
q.addFrom(Table1.TABLE1);

选项4 (在更高版本的jOOQ中添加):

Option 4 (added in a later version of jOOQ):

// For convenience, you can now specify several "SELECT" clauses
ctx.select(Table1.TABLE1.fields())
   .select(Table1.ID.count().as("IdCount")
   .from(Table1.TABLE1);

以上所有选项均使用 Table.fields() 方法,当然这依赖于这样的元数据信息在运行时出现,例如通过使用代码生成器。

All of the above options are using the Table.fields() method, which of course relies on such meta information being present at runtime, e.g. by using the code generator.

这篇关于如何从Jooq中的其他自定义(concat,sum,count)列获取数据库中的所有结果列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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