[英]How to get all the result columns from database with other custom(concat, sum, count) columns in Jooq
I have a table Table1 with 6 columns. 我有一个表有1列的表1。
Here is the sql statement that i need to map. 这是我需要映射的sql语句。
Select *,count(ID) as IdCount from Table1;
Now, the sql query result will be 7 columns ( 6 Table1 columns and 1 IdCount column). 现在,sql查询结果将是7列(6个Table1列和1个IdCount列)。 But when i implement the same in Jooq with this query, it only gets a single column "IDCount".
但是当我使用此查询在Jooq中实现相同时,它只获得一个列“IDCount”。
SelectQuery q = factory.selectQuery();
q.addSelect(Table1.ID.count().as("IdCount"));
q.addFrom(Table1.TABLE1);
Now, the resultant recordset have only a single column "IdCount" while what i need is all the columns and one additional column "IdCount". 现在,结果记录集只有一列“IdCount”,而我需要的是所有列和一个附加列“IdCount”。 I want 7 columns in Jooq too.
我也想在Jooq中使用7列。
Option 1 (using the asterisk): 选项1 (使用星号):
The *
(asterisk, star) operator has been added to jOOQ 3.11 through DSL.asterisk()
(unqualified asterisk) or through Table.asterisk()
(qualified asterisk). *
(星号,星号)运算符已通过DSL.asterisk()
(非限定星号)或通过Table.asterisk()
(限定星号)添加到jOOQ 3.11。 It can be used like any other column being projected. 它可以像投影的任何其他列一样使用。
Prior to jOOQ 3.11, there were the following other options as well: 在jOOQ 3.11之前,还有以下其他选项:
Option 2 (with the DSL syntax): 选项2 (使用DSL语法):
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);
Option 3 (with the "regular" syntax, which you used): 选项3 (使用“常规”语法):
SelectQuery q = factory.selectQuery();
q.addSelect(Table1.TABLE1.fields());
q.addSelect(Table1.ID.count().as("IdCount"));
q.addFrom(Table1.TABLE1);
Option 4 (added in a later version of jOOQ): 选项4 (在更高版本的jOOQ中添加):
// For convenience, you can now specify several "SELECT" clauses
ctx.select(Table1.TABLE1.fields())
.select(Table1.ID.count().as("IdCount")
.from(Table1.TABLE1);
All of the above options are using the Table.fields()
method, which of course relies on such meta information being present at runtime, eg by using the code generator. 所有上述选项都使用
Table.fields()
方法,该方法当然依赖于在运行时存在的这种元信息,例如通过使用代码生成器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.