我有一个挑战表和一个用户表,我希望为特定挑战返回每个用户的最短时间(我正在构建最快时间的排行榜)
所以,我有:
user_id, username
1, rita
2, sue
3, bob
challenge_id, user_id, time
1, 1, 10
1, 1, 20
2, 1, 5
1, 2, 5
1, 3, 30
1, 3, 50
我希望mySQL返回挑战1的结果:
sue, 5
rita, 10
bob, 30
我一直在摸索这个,我觉得它几乎是正确的,但它只返回一个结果,而不是全套:
SELECT u.user_id, username, ac.quickest_time AS intScore
FROM users u
JOIN
(
SELECT user_id, MIN(time_taken) AS quickest_time
FROM accepted_challenges
WHERE
challenges_id = 1
) ac ON ac.user_id = u.user_id
有什么想法吗?
答案 0 :(得分:1)
尝试类似:
SELECT username, MIN(time)
FROM MyTable1 m1 INNER JOIN MyTable2 m2
ON m1.user_id = m2.user_id
WHERE m2.challenge_id = 1
GROUP BY username