SELECT * 
from 
T a 
where 
not exists(select 1 from T where left(tname,charindex(',',tname))=left(a.tname,charindex(',',a.tname)) and ID>a.ID)
SELECT * 
from 
T a 
where 
not exists(select 1 from T where left(tname,charindex(',',tname))=left(a.tname,charindex(',',a.tname)) and ID<a.ID)

解决方案 »

  1.   

    select *
    from table a
    where not exists(select 1 from table where left(tname,4) = left(a.tname,4) and id < a.id)
      

  2.   


    declare @t table(id int,tname varchar(50))
    insert into @t select 10,'降魔录_sonyericsson-W958C系列游戏'
    insert into @t select 11,'降魔录_Nokia-7610系列游戏'
    insert into @t select 12,'降魔录_Nokia-N73系列游戏 '
    insert into @t select 13,'降33魔录_Nokia-N73系列游戏 '
    insert into @t select 14,'降33魔录_Nokia-N73系列游戏 '
    insert into @t select 15,'降33魔录_Nokia-N73系列游戏 'select * from @t a 
    where not exists
    (select 1 from @t where  id>a.id and
    substring(tname,1,charindex('_',tname)-1)=substring(a.tname,1,charindex('_',a.tname)-1))
      

  3.   


    或:
    declare @t table(id int,tname varchar(50))
    insert into @t select 10,'降魔录_sonyericsson-W958C系列游戏'
    insert into @t select 11,'降魔录_Nokia-7610系列游戏'
    insert into @t select 12,'降魔录_Nokia-N73系列游戏 '
    insert into @t select 13,'降33魔录_Nokia-N73系列游戏 '
    insert into @t select 14,'降33魔录_Nokia-N73系列游戏 '
    insert into @t select 15,'降33魔录_Nokia-N73系列游戏 'select * from @t a 
    where not exists
    (select 1 from @t where  id<a.id and
    substring(tname,1,charindex('_',tname)-1)=substring(a.tname,1,charindex('_',a.tname)-1))
      

  4.   

     发表于:2007-12-24 13:25:131楼 得分:0 
    SQL code
    SELECT * 
    from 
        T a 
    where 
        not exists(select 1 from T where left(tname,charindex('_',tname))=left(a.tname,charindex('_',a.tname)) and ID>a.ID)
    SELECT * 
    from 
        T a 
    where 
        not exists(select 1 from T where left(tname,charindex('_',tname))=left(a.tname,charindex('_',a.tname)) and ID<a.ID) 
     
      

  5.   

    roy_88 的方法能用,但效率很差,半天没反应,不知除了用charindex外可有其它方式?
      

  6.   

    好象只能用charindex()
    id   tname 
    10   降魔录_sonyericsson-W958C系列游戏 
    11   降魔录_Nokia-7610系列游戏 
    12   降魔录_Nokia-N73系列游戏 select * from tb where id in
    (select min(id) from tb group by left(tname,charindex('-')-1))
      

  7.   

    declare @t table(id int,tname varchar(50))
    insert into @t select 10,'降魔录_sonyericsson-W958C系列游戏'
    insert into @t select 11,'降魔录_Nokia-7610系列游戏'
    insert into @t select 12,'降魔录_Nokia-N73系列游戏 '
    insert into @t select 13,'降33魔录_Nokia-N73系列游戏 '
    insert into @t select 14,'降33魔录_Nokia-N74系列游戏 '
    insert into @t select 15,'降33魔录_Nokia-N75系列游戏 ' select distinct substring(tname,0,charindex('_',tname)) from @t---------------------------------------------
    降33魔录
    降魔录
      

  8.   

    10楼的,我取不止一个名字,那distinct能多字段吗?
      

  9.   

    不能,只能用in ,exists之类的,所以速度慢.
      

  10.   

    数据太多了,用roy_88的方法一直超时
      

  11.   

     create table #t (id int,tname varchar(50))
    insert into #t select 10,'降魔录_sonyericsson-W958C系列游戏'
    insert into #t select 11,'降魔录_Nokia-7610系列游戏'
    insert into #t select 12,'降魔录_Nokia-N73系列游戏 '
    insert into #t select 13,'降33魔录_Nokia-N73系列游戏 '
    insert into #t select 14,'降33魔录_Nokia-N74系列游戏 '
    insert into #t select 15,'降33魔录_Nokia-N75系列游戏 '
     
     select * from #t where id in (select min(id)   from #t group by substring(tname,0,charindex('_',tname)))