gpt4 book ai didi

php - 通过分组找到最大值

转载 作者:行者123 更新时间:2023-11-29 01:51:55 27 4
gpt4 key购买 nike

我有一个用户表:

uid country credits
1 1 10
2 1 8
3 2 4
4 2 6

我想找到按国家/地区分组的最高积分的用户。示例输出:

[0] => Array(
'country' => 1,
'uid' => 1,
'credits' => 10
),
[1] => Array(
'country' => 2,
'uid' => 4,
'credits' => 6
)

我试过(没成功):

SELECT 
U.*
FROM
`users` AS U
WHERE
U.user_deleted = 0
GROUP BY
U.country
HAVING
MAX(U.credits)

如何解决我的请求?

最佳答案

试试这个查询:

SELECT 
u.*
FROM
`users` AS u
WHERE
u.credits = (select max(u1.credits) from users u1 where u1.country=u.country group by u1.country)

关于php - 通过分组找到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259257/

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