select DISTINCT message from 表

解决方案 »

  1.   

    select max(message),phone from yourtable group by phone
      

  2.   

    select aa.* from 表 aa,(select phone,min(message) from 表 group by phone) bb
    where  aa.phone=bb.phone and aa.message=bb.message 
      

  3.   

    或者
    select * from 表 aa
    where message=(select top 1 message from 表 bb where phone=aa.phone)
      

  4.   

    给你个参考:
    --随机抽取分组后的数据,每组取N笔--示例数据
    declare @t table(id int,catgoryid int,name varchar(10))
    insert @t select 1 ,2,'a'
    union all select 2 ,2,'n'
    union all select 3 ,2,'h'
    union all select 4 ,2,'e'
    union all select 5 ,2,'d'
    union all select 6 ,3,'o'
    union all select 7 ,3,'t'
    union all select 8 ,3,'u'
    union all select 9 ,3,'c'
    union all select 10,3,'m'
    union all select 11,5,'k'
    union all select 12,5,'l'
    union all select 13,5,'q'
    union all select 14,5,'w'--查询
    select mid=identity(int),id=cast(id as int),catgoryid,name
    into #t from @t 
    order by newid()select * from #tselect id,catgoryid,name
    from #t a
    where mid in(select top 3 mid from #t where catgoryid=a.catgoryid)
    order by catgoryid,iddrop table #t/*--其中一次测试结果id          catgoryid   name       
    ----------- ----------- ---------- 
    1           2           a
    2           2           n
    3           2           h
    7           3           t
    9           3           c
    10          3           m
    11          5           k
    12          5           l
    14          5           w(所影响的行数为 9 行)
      

  5.   

    select distinct phone,message from 表 a where message = (select max(message) from 表 b where a.phone=b.phone)
    distinct 避免了相同号码的相同信息的重复显示
      

  6.   

    select message,phone from yourtable a
    where cast(a.message as varchar(200)+cast(a.phone as varchar(20)
    =(select top 1 cast(message as varchar(200)+cast(phone as varchar(20)
     from yourtable b where a.phone=b.phone order by message)
      

  7.   

    用Distinct者不对,我不是避免相同号码和
    相同信息,distinct是对选出的字段同时起作用
      

  8.   

    select message from table
    group by phone
      

  9.   

    select phone, (select top 1 message from table as A where A.phone=B.phone) as message
    from phone as B
    group by phone或者select distinct phone, (select top 1 message from table as A where A.phone=B.phone) as message from phone as B
      

  10.   

    select phone, (select top 1 message from table as A where A.phone=B.phone) as message
    from talbe as B
    group by phone或者select distinct phone, (select top 1 message from table as A where A.phone=B.phone) as message from table as B
      

  11.   

    select message=max(message),phone from tablename group by phone

    select message=min(message),phone from tablename group by phone
      

  12.   

    --如果要選出整條記錄
    ----------------------A.有關鍵字段(s)-----------------------------------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table1]
    create table table1(id int not null identity primary key,phone varchar(15) not null,message varchar(20) null,address varchar(50) null)goinsert into table1
    select '13512345678','aaa','a' union all
    select '13512345678','bbb','b' union all
    select '13512345678','ccc','c' union all
    select '13512345678','aaa','s' union all
    select '13512345671','bbb','j' union all
    select '13512345671','bbb','j' union all
    select '13512345671','ccf','f' union all
    select '13512345671','ccd','e' union all
    select '13112345671','aaa','d' 
    goselect * from table1
    /*
    id       phone             message  address
    1 13512345678 aaa a
    2 13512345678 bbb b
    3 13512345678 ccc c
    4 13512345678 aaa s
    5 13512345671 bbb j
    6 13512345671 bbb j
    7 13512345671 ccf f
    8 13512345671 ccd e
    9 13112345671 aaa d      
    */--方法1
    select a.* from table1 a join (select min(id) as id from table1 group by phone) b on a.id=b.id
    /*
    id       phone             message  address
    9 13112345671 aaa d
    5 13512345671 bbb j
    1 13512345678 aaa a
          
    */--方法2
    select a.* from table1 a where id in(select min(id) as id from table1 where a.phone=phone)
    /*
    id       phone             message  address
    9 13112345671 aaa d
    5 13512345671 bbb j
    1 13512345678 aaa a
          
    */----------------------A.有關鍵字段(s)---------------------------------------------------------B.沒有關鍵字段(s)-----------------------------------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table2]
    create table table2(phone varchar(15) not null,message varchar(20) null,address varchar(50) null)goinsert into table2
    select '13512345678','aaa','a' union all
    select '13512345678','bbb','b' union all
    select '13512345678','ccc','c' union all
    select '13512345678','aaa','s' union all
    select '13512345671','bbb','j' union all
    select '13512345671','bbb','j' union all
    select '13512345671','ccf','f' union all
    select '13512345671','ccd','e' union all
    select '13112345671','aaa','d' 
    goselect * from table2
    /*
    phone             message  address
    13512345678 aaa a
    13512345678 bbb b
    13512345678 ccc c
    13512345678 aaa s
    13512345671 bbb j
    13512345671 bbb j
    13512345671 ccf f
    13512345671 ccd e
    13112345671 aaa d
          
    */alter table table2 add  id int not null identity
    go--方法1
    select a.phone,a.message,a.address from table1 a join (select min(id) as id from table1 group by phone) b on a.id=b.id
    /*
    phone             message  address
    13112345671 aaa d
    13512345671 bbb j
    13512345678 aaa a
          
    */--方法2
    select  a.phone,a.message,a.address from table1 a where id in(select min(id) as id from table1 where a.phone=phone)
    /*
    phone             message  address
    13112345671 aaa d
    13512345671 bbb j
    13512345678 aaa a
          
    */alter table table2 drop column  id
    go
    ----------------------B.沒有關鍵字段(s)-----------------------------------