我试图在添加积分后获得用户的位置。到目前为止,我已经有了以下工作:
SELECT
u.userID
, u.username
, SUM(fp.finishingPositionPoints) AS totalPoints
FROM leagueinformation li
INNER JOIN users u ON u.userID = li.userID
INNER JOIN finishingPosition fp ON fp.finishingPositionID = li.finishingPositionID
WHERE li.leagueID='1'
GROUP BY li.userID
ORDER BY totalPoints DESC
以上代码产生如下内容:
userID | username | points
--------
1 | user1 | 150
2 | user2 | 127
3 | user3 | 100
-------------------------
从上面的示例中我将如何获得第二个用户位置(例如第二位)?
答案 0 :(得分:0)
您可以尝试使用此方法获取第二个数据,即添加LIMIT 1,1
SELECT
u.userID
, u.username
, SUM(fp.finishingPositionPoints) AS totalPoints
FROM leagueinformation li
INNER JOIN users u ON u.userID = li.userID
INNER JOIN finishingPosition fp ON fp.finishingPositionID = li.finishingPositionID
WHERE li.leagueID='1'
GROUP BY li.userID
ORDER BY totalPoints DESC
LIMIT 1, 1 --selects only second item