0

我正在使用 MySQL 5.7 并有以下查询:

SELECT 
    U.*,
    C.country,
    C.id AS country_id,
    C.iso
FROM 
    `users` AS U
LEFT JOIN
    `countries` AS C
ON 
    U.user_country_id = C.id
WHERE
    U.user_deleted = 0
GROUP BY
    U.user_country_id
HAVING 
    MAX(U.user_credits)

问题是:

未捕获的 PDOException:SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“dbname.U.id”;

我无法将我的组(错误结果)更改为:GROUP BY U.user_country_id, U.id

如何以正确的方式更改我的请求?谢谢!

4

1 回答 1

0

您可以通过这种方式获取具有 max(U.user_credits) 的行

SELECT 
    U.*,
    C.country,
    C.id AS country_id,
    C.iso
FROM 
    `users` AS U
LEFT JOIN
    `countries` AS C
ON 
    U.user_country_id = C.id
WHERE
   U.user_deleted = 0
AND   U.user_credits = ( select max(U.user_credits) from `users` as U)
于 2016-08-31T20:35:10.170 回答