例子:

     table 
   id   name 
   1     a 
   2     b 
   3     c 
   4     c 
   5     b 

比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录。


select distinct name from table 
得到的结果是
   name 
   a 
   b 
   c 
好像达到效果了,可是,我想要得到的是id值呢?改一下查询语句吧

select distinct name, id from table 
结果
   id   name 
   1       a
   2       b 
   3       c
   4       c
   5       b 
distinct怎么没起作用?作用是起了的,不过他同时作用了两个字段,也就是必须得idname都相同的才会被排除

我们再改改查询语句
select id, distinct name from table 
报错,distinct必须放在开头。

继续修改
select * from table group by name 

结果
   id    name 
   1       a 
   2       b 
   3       c 
成功!另外,group by 必须放在 order by  limit之前,不然会报错