gpt4 book ai didi

MySQL 5.7 - 分组依据没有工作

转载 作者:行者123 更新时间:2023-11-29 20:26:59 27 4
gpt4 key购买 nike

我使用的是 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)

问题是:

Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'dbname.U.id' which is not functionally dependent on columns in GROUP BY clause;

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

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

最佳答案

您可以通过这种方式获取具有 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)

关于MySQL 5.7 - 分组依据没有工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258442/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com