现在我遇到这么一个问题:
我使用这句sql语句的结果是对的:
select distinct c_id from table1
但是我使用了下面的语句就出现重复的c_id:
select distinct c_id,id from table1
其中id是自增长字段
请问这是什么问题
我使用这句sql语句的结果是对的:
select distinct c_id from table1
但是我使用了下面的语句就出现重复的c_id:
select distinct c_id,id from table1
其中id是自增长字段
请问这是什么问题
由于id是自增长的,主键,所以肯定不会有重复的,distinct对id去重是毫无意义的
等价于
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)