gpt4 book ai didi

mysql - 获取第1000分的用户(SQL查询)

转载 作者:行者123 更新时间:2023-11-29 11:50:12 25 4
gpt4 key购买 nike

我试图做一个查询来说明 school_id 1 的哪个学生获得了第 1000 分 - 在本例中是“Sharon”

+++++++++++++++++++++++++++++++++++++
# id # school_id # student # points #
# 1 # 1 # Harry # 100 #
# 2 # 1 # Bob # 200 #
# 3 # 1 # Jamie # 150 #
# 4 # 1 # Lee # 200 #
# 5 # 1 # John # 200 #
# 6 # 1 # Sharon # 170 #
# 7 # 2 # Tim # 2000 #
+++++++++++++++++++++++++++++++++++++

我对以下查询进行了破解,但它没有返回我期望的结果:

SELECT
SUM(points) as pointSum, student
FROM testing
GROUP BY school_id;

我希望有这样的结果......

++++++++++++++++++++++++++++++++++++++++
# id # school_id # student # pointsSum #
# 6 # 1 # Sharon # 1020 #
++++++++++++++++++++++++++++++++++++++++

最佳答案

使用派生表计算用户和之前用户的积分SUM(按 ID 排序)。

select name, sumpoints
from
(
select id, name, (select sum(points) from tablename t2
where t2.id <= t1.id) sumpoints AND t2.school_id = 1
from tablename t1
) dt
where sumpoints >= 1000
order by id
limit 1

关于mysql - 获取第1000分的用户(SQL查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34221299/

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