我有一张桌子上有人,他们的年龄,以及他们在每个年龄段的精彩。
最简单的查询是什么才能让John在最大年龄时“惊人”?
人
Name Age Awesomeness
Don 1 12
Don 2 23
Don 3 43
Don 4 30
Sam 1 9
Sam 2 18
Sam 3 59
Sam 4 99
我有最好的查询:
SELECT awesomeness
FROM people
JOIN (
SELECT MAX(age)
FROM people
WHERE name = 'Don'
) a
ON people.age = a.age
WHERE people.name = 'Don'
答案 0 :(得分:1)
只需使用order by
和limit
:
SELECT awesomeness
FROM people
WHERE people.name = 'Don'
ORDER BY age desc
LIMIT 1
答案 1 :(得分:0)
您可能希望在最大年龄时显示每个人的分数。
您可以使用此查询执行此操作:http://sqlfiddle.com/#!2/b0ff4/1/0
SELECT a.name, a.awesomeness
FROM people a
JOIN (
SELECT name, MAX(age) age
FROM people
GROUP by name
) b ON a.name = b.name AND a.age=b.age