自我加入子查询的问题;想知道下面2个查询的区别

时间:2016-07-06 18:55:04

标签: mysql sql oracle10g

这工作:

SELECT a.year, a.name, a.runs
from players a  JOIN
     (SELECT year, max(runs) runs FROM players GROUP BY year
     ) b  
     ON (a.year = b.year AND a.runs = b.runs);

这不起作用:

select a.name,b.year,b.run
from players a join 
     (select year, max(runs) as run, name from players group by year
     ) b
     on a.year = b.year AND a.runs = b.run;

有人可以帮我理解这个吗?

1 个答案:

答案 0 :(得分:0)

第二个加入查询未正确分组

     (select year, max(runs) as run, name from players group by year
 ) b

因为你也选择了你应该按名称分组的名称

        (select year, max(runs) as run, name from players group by year, name
 ) b

但这会给你不同的结果