码迷,mamicode.com
首页 > 数据库 > 详细

mysql中去重 distinct 用法

时间:2017-03-23 14:27:13      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:原因   其他   nts   mysq   fonts   log   tar   font   count   

在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段,例如有如下表user:

技术分享

用distinct来返回不重复的用户名:select distinct name from user;,结果为:

技术分享

这样只把不重复的用户名查询出来了,但是用户的id,并没有被查询出来:select distinct name,id from user;,这样的结果为:

技术分享

distinct name,id 这样的mysql 会认为要过滤掉name和id两个字段都重复的记录,如果sql这样写:select id,distinct name from user,这样mysql会报错,因为distinct必须放在要查询字段的开头。

所以一般distinct用来查询不重复记录的条数。

如果要查询不重复的记录,有时候可以用group by :

select id,name from user group by name;

mysql中去重 distinct 用法

标签:原因   其他   nts   mysq   fonts   log   tar   font   count   

原文地址:http://www.cnblogs.com/shiluoliming/p/6604407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!