我想在我的表中使用分页进行不同的选择,但它声称存在此错误。有谁知道如何解决它?
Error:
org.postgresql.util.PSQLException: ERROR:
for SELECT DISTINCT, ORDER BY expressions must appear in select list
@Query(value = "SELECT DISTINCT budget.* FROM budget LEFT JOIN user_budget ON budget.id = user_budget.budget_id ORDER BY budget.created DESC, ?#{#pageable}",
countQuery = "SELECT DISTINCT count(*) FROM budget LEFT JOIN user_budget ON budget.id = user_budget.budget_id",
nativeQuery = true)
public Page<Budget> findAllByfilter(Pageable pageable);
我知道这是一个老问题,但我想为正确的答案添加一些解释。问题更集中在 PostgreSQL 上DISTINCT ON
clause.
As the 文档 https://www.postgresql.org/docs/current/sql-select.html#SQL-DISTINCT says,
-
SELECT DISTINCT
子句消除重复行结果(整体元组)。
-
SELECT DISTINCT ON (your_column1, your_column2 ...)
消除匹配的行所有指定的表达式(指定的列对行进行排序并从结果集中删除重复项)。
为了保持返回结果集的顺序可预测,最好使用ORDER BY
clause.
N.B.如果你使用DISTINCT ON
条款与ORDER BY
表达式中,最左边的必须是ORDER BY
表达式需要匹配DISTINCT ON
表达。
更多详细信息请参见这个答案 https://stackoverflow.com/a/50847086/20666497.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)