对分组取最大值语句的疑惑??

ShineLC 2007-06-04 04:53:17
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)
...全文
313 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ShineLC 2007-06-05
  • 打赏
  • 举报
回复
恩,多谢各位~~特别是lwl0606(寒泉)

以前总认为not in 和not exists差不多
知道有区别,但没有实际感受过区别在哪?
ShineLC 2007-06-05
  • 打赏
  • 举报
回复
下面那句是 >= ,看错了,,我在想想
lwl0606 2007-06-05
  • 打赏
  • 举报
回复
楼主贴错了,
Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score>=a.score
)
这样是一条记录也不会显示的,因为 score>=a.score, 有一个等号,每个数都等于它自身,用了 not in,所以每行记录都不会显示

Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score>a.score
)
这样 会全部显示 ,因为每个数都不会大于它自身
明白??
create table t4 (uid int , email nvarchar(10), score int )
insert into t4 select
1 , 'aa', 10
union select 2 , 'aa', 20
union select 3 , 'bb', 60
union select 4 , 'bb', 50
union select 5 , 'cc', 40
union select 6 , 'cc', 80
union select 7 , 'aa', 15
cy36279211 2007-06-05
  • 打赏
  • 举报
回复
你的思路有问题, 你想一想.你的语句是不是每次那出一条数据来和整个表中的数据做比教取出score最大的一条.
ShineLC 2007-06-05
  • 打赏
  • 举报
回复
To: lwl0606(寒泉)

貌似全部显示出来的,,晕了

Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score>=a.score
)

uid email score
----------- -------------------------------------------------- -----------
1 aa 10
2 aa 20
3 bb 60
4 bb 50
5 cc 40
6 cc 80
7 aa 15
lwl0606 2007-06-05
  • 打赏
  • 举报
回复
Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score>=a.score
)
这样应该一行记录都不会显示出来
lwl0606 2007-06-05
  • 打赏
  • 举报
回复
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 这行记录会显示出来,以后的每一行都会这样。
mugua604 2007-06-05
  • 打赏
  • 举报
回复
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条件有值的话全语句才有值!!

ShineLC 2007-06-04
  • 打赏
  • 举报
回复
是不是我没能理解not exists的意思,,

和下面这个也不一样

Select * from T4 a where uid not in
(
Select uid From T4 where email=a.email and score>a.score
)
wgzaaa 2007-06-04
  • 打赏
  • 举报
回复
-_-||| 楼上回答.路过
lwl0606 2007-06-04
  • 打赏
  • 举报
回复
Select * from T4 where not exists
(Select 1 From T4 a,T4 b where a.email=b.email and b.score>a.score)

这个条件 等于没加,因为 没有吧T4 的条件加进去,而是判断的a 和b 的条件
fa_ge 2007-06-04
  • 打赏
  • 举报
回复
这是反向思维
lwl0606 2007-06-04
  • 打赏
  • 举报
回复
Select * from T4 a where not exists
(Select * From T4 where email=a.email and score>a.score)

在 T4 找 email 相等 并且不存在 score 比它大的 值,就是再没有 score 比它大,既最大值
肥胖的柠檬 2007-06-04
  • 打赏
  • 举报
回复
-_-||| 楼下回答.路过

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧