![](/img/trans.png)
[英]How to get distinct values of a single column in Criteria API(JPA)
[英]How to get the distinct values in a column as well as total count using spring jpa pagination
我正在尝试 select 列 C 的唯一值列表以及相应的计数。 我的代码如下
@Query(value = "SELECT DISTINCT t.C FROM table t WHERE t.param = :param order by t.C",
countQuery = "SELECT count(DISTINCT t.C) FROM table t WHERE t.param = :param")
Page<String> findUniqueWithCountPagination(Pageable pageable,
@Param("param") String param);
ORDER BY items must appear in the select list if SELECT DISTINCT is specified
。 当我启用 show-sql 属性时,我注意到 Spring 正在按过滤器自动将另一列 U 添加到 order 中。 U 是表 t 中的时间戳列。
如何删除 order by 中不必要的字段 U? 如果这是不可能的,还有其他方法可以为我的要求编写查询吗?
我只需要 C 列中的唯一值并且它是计数的,因此我对 C 本身应用了排序并将其从查询中删除。 order by
在执行期间自动应用。 代码片段如下:
Pageable pageable = PageRequest.of(filterOption.getPageNo(), filterOption.getPageSize(), Sort.Direction.DESC, C);
// repository class
@Query(value = "SELECT DISTINCT t.C FROM table t WHERE t.param = :param",
countQuery = "SELECT count(DISTINCT t.C) FROM table t WHERE t.param = :param")
Page<String> findUniqueWithCountPagination(Pageable pageable,
@Param("param") String param);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.