(非原创)、mysql
在使用mysql时,有时须要查询出某个字段不重复的记录,这时能够使用mysql提供的distinct这个关键字来过滤重复的记录,可是实际中咱们每每用distinct来返回不重复字段的条数(count(distinct id)),其缘由是distinct只能返回他的目标字段,而没法返回其余字段,例若有以下表user:sql
用distinct来返回不重复的用户名:select distinct name from user;,结果为:3d
这样只把不重复的用户名查询出来了,可是用户的id,并无被查询出来:select distinct name,id from user;,这样的结果为:blog
distinct name,id 这样的mysql 会认为要过滤掉name和id两个字段都重复的记录,若是sql这样写:select id,distinct name from user,这样mysql会报错,由于distinct必须放在要查询字段的开头。select
因此通常distinct用来查询不重复记录的条数。im
若是要查询不重复的记录,有时候能够用group by :d3
select id,name from user group by name;查询