有时需要查询出某个字段不重复的记录,这时可以使用Mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回
有时需要查询出某个字段不重复的记录,这时可以使用Mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段
distinct这种方式会将全部内容存储在一个hash结构里,最后通过计算hash结构中key的个数即可得到结果,是以空间换取时间的方式
select distinct name from user
#查询出不重复的用户名,返回结果只有一个name字段,没有其他字段
select id,distinct name from user
#mysql会报错,因为distinct必须放在要查询字段的开头
select distinct name,id from user
#mysql 会认为要过滤掉name和id两个字段都重复的记录
一般distinct用来查询不重复记录的条数(count(distinct id))
如果要查询不重复的记录,可以用group by
select id,name from user group by name;
#name字段去重,结果中有id和name两个字段
来源地址:https://blog.csdn.net/m1992222/article/details/131642850
--结束END--
本文标题: Mysql 数据库去重(distinct)
本文链接: https://www.lsjlt.com/news/441768.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0