现在我遇到这么一个问题:
我使用这句sql语句的结果是对的:
select distinct c_id from table1
但是我使用了下面的语句就出现重复的c_id:
select distinct c_id,id from table1
其中id是自增长字段
请问这是什么问题

解决方案 »

  1.   

    distinct c_id,id 这种方式,只能确保是c_id与id,两个字段不重复。
      

  2.   

    select distinct(c_id),id from table1
      

  3.   

    distinct是去重的意思,看你要对哪个字段去重 不能一概的都distinct
    由于id是自增长的,主键,所以肯定不会有重复的,distinct对id去重是毫无意义的
      

  4.   

    select   distinct   c_id,id   from   table1 
    等价于
    select   distinct   (c_id,id)   from   table1 由于id是自增长字段,所以(c_id,id)是不会有重复的,但c_id有可能重复查询改成这样就不会有重复的c_id了:
    select c_id,id  
    from table1 t
    where not exists(select * from table1 where c_id=t.c_id and id<t.id)