我正在嘗試選擇 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?如果這是不可能的,有沒有其他方法可以根據我的要求撰寫查詢?
uj5u.com熱心網友回復:
我只需要列 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369197.html
