mysql中distinct和group by过滤删除重复行

小明SEO博客 小明SEO博客,新时代SEO博客

在mysql中distinct就是可以直接去重的而group by 是分组显示的,但是有朋友在应用中可能会发现distinct并不像官方讲得那有实用了,下面我来介绍一下它们是怎么过滤删除重复行.

下面先来看看例子,代码如下:

  1. table
  2. idname
  3. 1a
  4. 2b
  5. 3c
  6. 4c
  7. 5b

库结构大概这样,这只是一个简单的例子,实际情况会复杂得多,比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录,代码如下:

select distinct name from table

得到的结果是:

  1. name
  2. a
  3. b
  4. c

好像达到效果了,可是,我想要得到的是id值呢?改一下查询语句吧:

select distinct name, id from table

结果会是:

  1. idname
  2. 1a
  3. 2b
  4. 3c
  5. 4c
  6. 5b

distinct怎么没起作用?作用是起了的,不过他同时作用了两个字段,也就是必须得id与name都相同的才会被排除.

我们再改改查询语句:select id, distinct name from table

现在将完整语句放出:

select *, count(distinct name) from table group by name

结果:

  1. idnamecount(distinctname)
  2. 1a1
  3. 2b1
  4. 3c1

上面简单但有些地方是不能完成我们的需要的,下面记录了些常用的重复记录操作语句

查询及删除重复记录的方法.

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,代码如下:

  1. select*frompeople
  2. wherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录,代码如下:

  1. deletefrompeople
  2. wherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)
  3. androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)

3、查找表中多余的重复记录,多个字段,代码如下:

  1. select*fromvitaea
  2. where(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)

4、删除表中多余的重复记录,多个字段,只留有rowid最小的记录,代码如下:

  1. deletefromvitaea
  2. where(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)
  3. androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)

5、查找表中多余的重复记录,多个字段,不包含rowid最小的记录,代码如下:

  1. select*fromvitaea
  2. where(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)
  3. androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)--phpfensi.com

相关广告
  • mysql中distinct和group by过滤删除重复行 mysql中distinct和group by过滤删除重复行 mysql中distinct和group by过滤删除重复行
相关阅读

mysql中distinct和group by过滤删除重复行

2019/10/10 17:35:10 | 谷歌SEO算法 | 谷歌CEO