我在一个表中保存了多个值。每个值对应一年,例如:
YEAR | NAME
2014/2015 | Udine
2014/2015 | Firence
2015/2016 | Milan
2015/2016 | Chievo
在我的查询中,我希望返回具有最大年份的所有行,特别是2015/2016。所以我写道:
SELECT MAX( years ) , name
FROM teams
WHERE country = 'Italy'
AND league LIKE 'Serie A%'
但是此查询返回最后一行而不是所有可用行。
我在等这个结果=> Milan, Chievo
注意:表值只是一个例子
答案 0 :(得分:0)
只有当该团队不存在以后的行时,才会NOT EXISTS
返回一行:
SELECT years, name
FROM teams t1
WHERE t1.country = 'Italy'
AND t1.league LIKE 'Serie A%'
AND NOT EXISTS (select 1 from teams t2
WHERE t2.country = 'Italy'
AND t2.league LIKE 'Serie A%'
AND t2.name = t1.name
AND t2.years > t1.years)
答案 1 :(得分:0)
试
SELECT t1.years, t1.name
FROM teams t1
join teams t2 on t2.years = t1.years
where t1.years = (select max(t3.years) from teams t3)
group by t1.name