![](/img/trans.png)
[英]Error related to only_full_group_by when executing a query in MySql on Server, working fine on local machine
[英]Spring Boot generates invalid MySQL query (only_full_group_by)
我正在使用Spring Boot 1.5.2和以下@Query:
@Query(value = "SELECT m FROM Message m WHERE m.from.userId = :myId OR m.to.userId = :myId GROUP BY m.from.userId, m.to.userId ORDER BY m.date DESC")
List<Message> chatOverview(@Param("myId") final Long myUserId);
該查詢的目的是創建一個聊天即時通訊概述,您可以在其中查看所進行的每個對話的最后一條消息。 對於開發人員來說,它工作正常,但在生產環境(較新的MySQL數據庫版本)中,出現此錯誤:
java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'message0_.message_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
我在此線程中閱讀了進行此更改的原因是什么,但是我找不到用JPA / Spring修復此問題的方法。 我無法更改生產MySQL數據庫中的任何設置,並且我也希望避免在Spring中進行任何升級。 如何解決查詢?
這是GROUP BY (請參見4.7節)子句的定義和目的:
GROUP BY構造允許根據一組屬性聚合結果值。
這意味着僅當您匯總(求和,平均,最大值,最小值,...)字段的值時才使用它。 但是在您的情況下,我看不到任何聚合函數。 因此,只需刪除GROUP BY
子句,一切就可以了:
SELECT m FROM Message m WHERE m.from.userId = :myId OR m.to.userId = :myId ORDER BY m.date DESC
對userId
進行分組也沒有意義,因為此查詢返回的所有實體在此字段中將具有相同的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.