日期:2014-05-19  浏览次数:20427 次

对分组取最大值语句的疑惑??
Select   *   from   T4   a   where   not   exists
(Select   *   From   T4   where   email=a.email   and   score> a.score)

这个语句是求以email分组后中的score最大值~`看不太明白,请达人讲解一下,谢谢!

和下面这句为什么会不同呢?

Select   *   from   T4   where   not   exists
(Select   1   From   T4   a,T4   b   where   a.email=b.email   and   b.score> a.score)

------解决方案--------------------
这是反向思维
------解决方案--------------------
Select * from T4 where not exists
(Select 1 From T4 a,T4 b where a.email=b.email and b.score> a.score)--这个跟主表T4没关系


Select * from T4 a where not exists
(Select * From T4 where email=a.email and score> a.score)---T4 a拿出一条记录跟T4对比, email=a.email and score> a.score条件有值的话全语句才有值!!


------解决方案--------------------
Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score> a.score
)
这个条件应该 等于也是没加,比如 uid 为1 email 为 1 ,score 为 80 ,外层的Select 走到这一行记录的时候会在嵌套的语句中寻找email 为 1 ,score> 80 的uid ,其中肯定不包括 uid 为1,因为自身不可能大于自身,uid 不在这个范围,符合条件,uid 为1 这行记录会显示出来,以后的每一行都会这样。

------解决方案--------------------
你的思路有问题, 你想一想.你的语句是不是每次那出一条数据来和整个表中的数据做比教取出score最大的一条.