select  * from tb a where not exists(select 1 from tb where uname=a.uname and score>a.score)

解决方案 »

  1.   

    一发贴格式都乱了,我再整理一下姓名 分数 地址 备注 
    ----------------------------------------- 
    mary  40  sz  thanks 
    kate  45  uy  sorry 
    moss  28  sh  hello 
      

  2.   

    create table tb(uname varchar(10), score int, location varchar(10) ,    re varchar(10) )
    goinsert tb select 'mary' ,30, 'sh'  ,       'hello' 
    insert tb select 'mary', 40, 'sz'  ,       'thanks' 
    insert tb select 'kate' ,24, 'sd'  ,       'hi' 
    insert tb select 'kate' ,45, 'uy'  ,       'sorry' 
    insert tb select 'moss', 28, 'sh'  ,       'hello' 
    insert tb select 'moss', 0, 'sz'   ,      'hi' 
    go
    select  * from tb a where not exists(select 1 from tb where uname=a.uname and score>a.score)
    go
    drop table tb
    go
    /*
    uname      score       location   re     
    ---------- ----------- ---------- ---------- 
    mary       40          sz         thanks
    kate       45          uy         sorry
    moss       28          sh         hello(所影响的行数为 3 行)*/
      

  3.   

    用辅助列方法
    create table tb(uname varchar(10), score int, location varchar(10) ,    re varchar(10) )
    goinsert tb select 'mary' ,30, 'sh'  ,       'hello' 
    insert tb select 'mary', 40, 'sz'  ,       'thanks' 
    insert tb select 'kate' ,24, 'sd'  ,       'hi' 
    insert tb select 'kate' ,45, 'uy'  ,       'sorry' 
    insert tb select 'moss', 28, 'sh'  ,       'hello' 
    insert tb select 'moss', 0, 'sz'   ,      'hi' 
    go
    select  * from tb a WHERE score = (SELECT MAX(score) FROM tb WHERE uname = a.uname) ORDER BY uname
    go
    drop table tb
    go
    /*
    uname      score       location   re
    ---------- ----------- ---------- ----------
    kate       45          uy         sorry
    mary       40          sz         thanks
    moss       28          sh         hello*/
      

  4.   

    LS不正确,如果有多个UNAME有相同的SCORE,数据出错!
      

  5.   

    create table #tb(uname varchar(10), score int, location varchar(10) ,    re varchar(10) )
    insert #tb select 'mary',30,'sh','hello' 
    insert #tb select 'mary',40,'sz','thanks' 
    insert #tb select 'kate',24,'sd','hi' 
    insert #tb select 'kate',45,'uy','sorry' 
    insert #tb select 'moss',28,'sh','hello' 
    insert #tb select 'moss',0,'sz','hi' 
    go
    select  * from #tb a where exists(select 1 from #tb where uname=a.uname and score<a.score)uname      score       location   re
    ---------- ----------- ---------- ----------
    mary       40          sz         thanks
    kate       45          uy         sorry
    moss       28          sh         hello(3 行受影响)