获取第 1000 个点的用户(SQL 查询)
Get the user with the 1000th point (SQL query)
我正在尝试做一个查询,以说明来自 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 #
+++++++++++++++++++++++++++++++++++++
我遇到了以下查询问题,但它return 不是我所期望的:
SELECT
SUM(points) as pointSum, student
FROM testing
GROUP BY school_id;
我希望得到这样的结果...
++++++++++++++++++++++++++++++++++++++++
# id # school_id # student # pointsSum #
# 6 # 1 # Sharon # 1020 #
++++++++++++++++++++++++++++++++++++++++
使用派生的 table 计算用户和之前用户(id-wise)的 SUM
点数。
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
我正在尝试做一个查询,以说明来自 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 #
+++++++++++++++++++++++++++++++++++++
我遇到了以下查询问题,但它return 不是我所期望的:
SELECT
SUM(points) as pointSum, student
FROM testing
GROUP BY school_id;
我希望得到这样的结果...
++++++++++++++++++++++++++++++++++++++++
# id # school_id # student # pointsSum #
# 6 # 1 # Sharon # 1020 #
++++++++++++++++++++++++++++++++++++++++
使用派生的 table 计算用户和之前用户(id-wise)的 SUM
点数。
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