gpt4 book ai didi

java - Jooq 如何区分或 countDistinct 多于一列

转载 作者:行者123 更新时间:2023-11-29 05:34:13 26 4
gpt4 key购买 nike

我发现Factory类中的distinct函数只接受一个参数

public static AggregateFunction<Integer> countDistinct(Field<?> field);

并且没有像distinct(Field... fields)这样的函数

我想表达这样一条SQL语句:

select c1, count(distinct c2,c3) from t1 group by c1;

PS:有一个非 dsl api:SelectQuery.setDistinct(true),但它会“区分”所有列,这不是我想要的。

最佳答案

这很有趣。据我所知,这在 SQL 中是不可能的。 SQL:2008标准规定

10.9 <aggregate function>

Format
<aggregate function> ::=
COUNT <left paren> <asterisk> <right paren> [ <filter clause> ]
| <general set function> [ <filter clause> ]

<general set function> ::=
<set function type> <left paren> [ <set quantifier> ]
<value expression> <right paren>

逗号分隔的参数列表没有空间,有或没有 DISTINCT设置量词。这在 Oracle 中正确实现:

enter image description here

然而,MySQL 似乎允许这种非标准的“方便语法”:

COUNT(DISTINCT expr,[expr...])

Returns a count of the number of rows with different non-NULL expr values.

(取自 http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_count )。

为了回答你的问题,这在 jOOQ 中目前是不可能的,尽管我提交了 #1728为将来的支持。目前,您可以通过编写自己的多参数聚合函数支持来解决此问题:

Field<Integer> myCount = Factory.field(
"count(distinct {0}, {1})", Integer.class, c2, c3);

参见 Factory.field(String, Class<T>, QueryPart...) 的相关 Javadoc方法详解

现在在 jOOQ 2.6 中实现了:

你可以写

Field<Integer> count = Factory.countDistinct(c2, c3);

请参阅 Factory.countDistinct(Field...) 的 Javadoc方法,详见

关于java - Jooq 如何区分或 countDistinct 多于一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com