比如我有一个表    main_table
   
    id     name
     1      aa
     2      bb
     3      cc
     4      aa
     5      dd
     6      ee希望查询出来的表是
   name
    aa
    bb
    cc
    dd
    ee让重复的数据只出现一次谢谢大家,能帮忙看看吗?

解决方案 »

  1.   

    select distinct name from table1
      

  2.   

    select distinct name from main_table 
      

  3.   

    select distinct name from main_table  order by name
    select name from main_table group by name order by name
      

  4.   

    select distinct name from table1

    select name from table1 group by name
      

  5.   

    谢谢,如果是    main_table 
        
        id     name   other
         1      aa     00
         2      bb     00
         3      cc     00
         4      aa     00
         5      dd     00
         6      ee     00查询出来的表是
        
         name   other
          aa     00
          bb     00
          cc     00
          dd     00
          ee     00只对name进行不能重复的判断呢?
      

  6.   

    select name,min(other)as other from table1 group by name
      

  7.   

    如果Other栏位的值一致的话,即如name ='aa' ,对应的Other都为同一个值,SELECT NAME ,OTHER FROM # GROUP BY NAME,OTHER ORDER BY NAME , OTHER如果Other栏位的值不一致,那么就需要使用相应的group函数,取Other的Max,Min等.SELECT NAME ,Max(OTHER) FROM # GROUP BY NAMEORDER BY NAME
      

  8.   

    楼主看一下,也许对你有用!  
    create table tb(id  int,   name varchar(10),  other varchar(10))
    insert into tb
    select      1,      'aa','00' union all 
    select      2,      'bb','00' union all 
    select      3,      'cc','00' union all 
    select      4,      'aa','00' union all 
    select      5,      'dd','00' union all 
    select      6,      'ee','00' select * from tb a where not exists (select 1 from tb where name=a.name and id<a.id)/*
    id      name    other
    -------------------------
    1 aa 00
    2 bb 00
    3 cc 00
    5 dd 00
    6 ee 00
    */drop table tb
      

  9.   

    谢谢jinjazz ,记录不需要一样多,只是要求name有重复字段的消息就不显示了这里还有些不清楚,对于上面这个SQL语句他是怎么分组的,
    因为other的数据都是重复的,我有点糊涂了
      

  10.   


    先按 NAME分组,再按OTHER分组,  楼主可以先看一下联机丛书 GROUP BY  的用法!
      

  11.   

    select distinct name,other from main_table就可以了
      

  12.   

    谢谢大家,我明白了distinct 应该出去相同的行吧?
      

  13.   

    main_table 
        
        id     name 
         1      aa 
         2      bb 
         3      cc 
         4      aa 
         5      dd 
         6      ee 希望查询出来的表是 
       name 
        aa 
        bb 
        cc 
        dd 
        ee 
    select distinct name from tb order by name
    如果是     main_table  
         
        id     name   other 
         1      aa     00 
         2      bb     00 
         3      cc     00 
         4      aa     00 
         5      dd     00 
         6      ee     00 查询出来的表是 
         
         name   other 
          aa     00 
          bb     00 
          cc     00 
          dd     00 
          ee     00 
    select distinct name ,other from tb order by name如果是     main_table  
         
        id     name   other 
         1      aa     00 
         2      bb     00 
         3      cc     00 
         4      aa     00 
         5      dd     00 
         6      ee     00 查询出来的表是 
         
    id     name   other 
    1      aa     00 
    2      bb     00 
    3      cc     00 
    5      dd     00 
    6      ee     00 
    select t.* from main_table t where id = (select min(id) from main_table where name = name) order by t.name如果是     main_table  
         
        id     name   other 
         1      aa     00 
         2      bb     00 
         3      cc     00 
         4      aa     00 
         5      dd     00 
         6      ee     00 查询出来的表是 
         
    id     name   other 
    4      aa     00 
    2      bb     00 
    3      cc     00 
    5      dd     00 
    6      ee     00 
    select t.* from main_table t where id = (select max(id) from main_table where name = name) order by t.name