表 worksUser   Title   Image   Islock
张三   274     34534    3
张三   224     34534    3
李四   284     34534    3
李四   254     34534    3
张三   294     34534    3我这样写不行?搞了一下午
写到这了 select user from works where IsLock=3 group by User 。我想要这样的结果:
表 works
张三   274     34534    3
李四   284     34534    3大家帮我写一条语句,谢谢`~

解决方案 »

  1.   

    select User,max(Title),Image,Islock from work group by User,Image,Islock 
      

  2.   

    select distinct  user from works where IsLock=3 group by User 
      

  3.   


    create table #TT
    (
     UserName varchar(20),
     Title varchar(20),
    Image varchar(20),
     Islock int
    )
    insert into #TT select '张三','274','34534',3
    union all select '张三','224','34534',3
    union all select '李四','284','34534',3
    union all select '李四','254','34534',3
    union all select '张三','294','34534',3select UserName,max(Title) Title,[Image],Islock from #TT group by UserName,[Image],Islock UserName             Title                Image                Islock
    -------------------- -------------------- -------------------- -----------
    李四                   284                  34534                3
    张三                   294                  34534                3
      

  4.   


     select works.* from works inner join (
       select [user] , max(Title) as Title   from works where IsLock=3 group by User ) a 
       on a.[user] = works.[user]  and a.Title = works.Title
      

  5.   


    你的那个Title是最大值还是最小值啊
      

  6.   

    declare @t table([User] varchar(10),Title int,[Image] int,Islock int)
    insert @t select '张三',274,34534,3 
    union all select '张三',224,34534,3 
    union all select '李四',284,34534,3
    union all select '李四',254,34534,3 
    union all select '张三',294,34534,3select * from @t a
    where not exists(select 1 from @t where [User]=a.[User] and title>a.title)
    /*(所影响的行数为 5 行)User       Title       Image       Islock      
    ---------- ----------- ----------- ----------- 
    李四         284         34534       3
    张三         294         34534       3(所影响的行数为 2 行)
    */
      

  7.   

    select max(distinct(user)) from workers where islock=3 group by username
      

  8.   

    不行?查不了,提示TITLE错误?
      

  9.   

    谢谢 cailee ,解决了~~
      

  10.   

    create table #TT1
    (
      ID int identity(1,1) primary key,
     UserName varchar(20),
     Title varchar(20),
    Image varchar(20),
     Islock int
    )
    insert into #TT1 select '张三','274','34534',3
    union all select '张三','224','34534',3
    union all select '李四','284','34534',3
    union all select '李四','254','34534',3
    union all select '张三','294','34534',3select T1.UserName,T1.Title,T1.[Image],T1.Islock from #TT1 T1 right join
    (
    select min(ID) ID,UserName from #TT1
     group by UserName
    ) T2
    on T2.ID=T1.ID
    UserName             Title                Image                Islock
    -------------------- -------------------- -------------------- -----------
    李四                   284                  34534                3
    张三                   274                  34534                3
      

  11.   

    如果你的Islock 都一样declare @t table([User] varchar(10),Title int,[Image] int,Islock int)
    insert @t select '张三',274,34534,3 
    union all select '张三',224,34534,3 
    union all select '李四',284,34534,3
    union all select '李四',254,34534,3 
    union all select '张三',294,34534,3;with tb as(
    select id=row_number() over(order by Islock),* from @t)
    select [User] ,Title ,[Image],Islock from tb a
    where not exists(select 1 from tb where a.[User]=[User] and id<a.id)
    --(5 行受影响)
    --User       Title       Image       Islock
    ------------ ----------- ----------- -----------
    --张三         274         34534       3
    --李四         284         34534       3
    --
    --(2 行受影响)
      

  12.   

    楼主的问题没说清楚,关键就是他的title要求是max的
      

  13.   

    题目说的不是很清楚~  根据结果看同名的title取得也不是最大值
      

  14.   

    easy!以前我写过类似的。不过,貌似被人抢先了,我就不说咯,呵呵
      

  15.   

    哎,楼主,看了半天,我给你写一条菜鸟都看得懂的语句吧
    select * from works where([user]='张三' and Title=274) or ([user]='李四' and Title=284)
    这就是你要的结果,通过测试了的哦!
      

  16.   


    这条语句可以,但是要是Title都一样?
      

  17.   

    select * from works where ([user]='张三' and Title=274) or ([user]='李四' and Title=284)  这条语句最适合题意了。并不是求每个用户的最大值,只是求其中的某个特定值
      

  18.   

    语句:SELECT *
    FROM works AS a
    WHERE not exists(select 1 from works where [User]=a.[User] and title>a.title) and islock=3 and Typelist=17大家测试的时候把数据表里面Typelist里面的18改成17试试~~,郁闷死~~
      

  19.   


    declare @t table([User] varchar(20),  Title int ,  [Image]  int ,  Islock  int )
    insert into @t values('张三' , 274  ,  34534  ,  3)
    insert into @t values('张三' , 224  ,  34534  ,  3)  
    insert into @t values('李四' , 284  ,  34534  ,  3) 
    insert into @t values('李四' , 254  ,  34534  ,  3) 
    insert into @t values('张三' , 294  ,  34534  ,  3)
    select a.[User],  (select top 1 Title from @t where [User] = a.[User]) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User]) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User]) as  [Islock] 
    from @t a  group by a.[User]
    lz是不是想要这样的结果
      

  20.   

    islock=3 and Typelist=17  这两条件怎么加进去啊?
      

  21.   

    group by 后面增加where语句就行了
      

  22.   


    declare @t table([User] varchar(20),  Title int ,  [Image]  int ,  Islock  int )
    insert into @t values('张三' , 274  ,  34534  ,  3)
    insert into @t values('张三' , 224  ,  34534  ,  3)  
    insert into @t values('李四' , 284  ,  34534  ,  3) 
    insert into @t values('李四' , 254  ,  34534  ,  3) 
    insert into @t values('张三' , 294  ,  34534  ,  3)select a.[User],  (select top 1 Title from @t where [User] = a.[User]) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User]) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User]) as  [Islock] 
    from @t a   where a.islock=3  group by a.[User]  
    类似这样的。楼上说错了
      

  23.   

    select a.[User],  (select top 1 Title from @t where [User] = a.[User]) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User]) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User]) as  [Islock] 
    from @t a   where a.islock=3 and a.typelist=17  group by a.[User]  这样提示不行啊?
      

  24.   

    a.typelist 不起作用,mbh0210麻烦再看看~~
      

  25.   

    我换了~~select a.[User], (select top 1 Title from works where [User] = a.[User]) as Title,(select top 1  [typelist]  from works where [User] = a.[User]) as  [typelist],(select top 1 [Imgurl]  from works where [User] = a.[User]) as [Imgurl] from works a where a.islock=3 and a.[typelist]=17 group by a.[User]
      

  26.   


    declare @t table([User] varchar(20),  Title int ,  [Image]  int ,  Islock  int, typelist int  )
    insert into @t values('张三' , 274  ,  34534  ,  3 , 17)
    insert into @t values('张三' , 224  ,  34534  ,  3, 17)  
    insert into @t values('李四' , 284  ,  34534  ,  3, 17) 
    insert into @t values('李四' , 254  ,  34534  ,  3, 17) 
    insert into @t values('张三' , 294  ,  34534  ,  3, 17)select a.[User],  (select top 1 Title from @t where [User] = a.[User]) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User]) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User]) as  [Islock] ,
    (select top 1  [typelist]  from @t where [User] = a.[User]) as  [typelist] 
    from @t a   where a.islock=3 and a.typelist =17  group by a.[User]  
    正常的啊,你写的也没有问题啊,在检查一下
      

  27.   

    可以写成类似这样的,主要是没有加过滤条件
    select a.[User]  ,
    (select top 1 Title from @t where [User] = a.[User] and islock=3 and typelist=17 ) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User] and islock=3 and typelist=17) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User] and islock=3 and typelist=17 ) as  [Islock] ,
    (select top 1  [typelist]  from @t where [User] = a.[User] and islock=3 and typelist=17 ) as  [typelist] 
    from @t a   where a.islock=3 and a.typelist =17  group by a.[User]
      

  28.   


    select * from (select a.[User],  (select top 1 Title from @t where [User] = a.[User]) as Title,
    (select top 1  [Image]  from @t where [User] = a.[User]) as  [Image] ,
    (select top 1  [Islock]  from @t where [User] = a.[User]) as  [Islock] ,
    (select top 1  [typelist]  from @t where [User] = a.[User]) as  [typelist] 
    from @t a group by a.[User]) as b where b.typelist=17  这样可以,但是提示数据错误,郁闷啊~~