[英]Problems with aggregate SQL functions in Spring Boot application
我在 Spring Boot 中的網站上工作,該引導連接到 MySQL db。 在數據庫中,我有兩個表: Player
和Match
,我創建了一個查詢,該查詢應該返回一個包含他們已經玩過的比賽計數的球員列表。 問題是鍵入的聚合 function count(M)
沒有,我不知道我做錯了。 在 db 中,我有例如id
= 1 的Player
和兩個玩Match
es,另一個有一個Match
,另一個有 0 。結果我得到的是一個Player
有 3 個玩Match
es。 如果我輸入 M.id 而不是 count(M),我會為Player
1 獲得兩行(一個用於Match
id),第二行是 onw 行。 我的代碼有什么問題?
@Query( "select new dto.PlayerDTO(" +
" P.id, " +
" P.nickname, " +
" count(M), " +
"from " +
" Player P left join Match M on P = M.player " +
"where " +
" P.games like %?1% ")
List<PlayerDTO> findPlayersForGame(String game);
當您在連接表上count()
時,您必須使用group by
語句:
@Query( "select new dto.PlayerDTO(" +
" P.id, " +
" P.nickname, " +
" count(M), " +
"from " +
" Player P left join Match M on P = M.player " +
"where " +
" P.games like %?1% " +
"group by P.id ")
List<PlayerDTO> findPlayersForGame(String game);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.