mysql如何去除两个字段数据相同的记录

mysql如何去除两个字段数据相同的记录,第1张

MySQL查询重复字段,及删除重复记录的方法

数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较。如果仅仅是查找数据库中name不重复的字段,很容易:

SELECT min(`id`),`name` FROM `table` GROUP BY `name`

但是这样并不能得到说有重复字段的id值。(只得到了最小的一个id值)查询哪些字段是重复的也容易:

SELECT `name`,count(`name`) as count FROM `table` GROUP BY `name` HAVING count(`name`) >1 ORDER BY count DESC

但是要一次查询到重复字段的id值,就必须使用子查询了,于是使用下面的语句。

SELECT `id`,`name` FROM `table` WHERE `name` in (

SELECT `name`

FROM `table`

GROUP BY `name` HAVING count(`name`) >1)

但是这条语句在mysql中效率太差,感觉mysql并没有为子查询生成零时表。于是使用先建立零时表:

create table `tmptable` as (

SELECT `name`

FROM `table`

GROUP BY `name` HAVING count(`name`) >1)

然后使用多表连接查询:

SELECT a.`id`, a.`name` FROM `table` a, `tmptable` t WHERE a.`name` = t.`name`

结果这次结果很快就出来了。

========================

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

(一)

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

select * from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)

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

delete from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

3、查找表中多余的重复记录(多个字段)

select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)

and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)

and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

select * from 表名 where match_id in (select match_id from 表名 where team_id='a' or team_id='b' group by match_id)

select a,b,c from table where b in (selct distinct b from table where c='3e') order by b

这个是按照这个格式输出

5 d 3e

6 d 3e

4 e 3e

7 e 3e

1 w 3e

2 w 3e

另外你的意思描述不是很清楚


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8366209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-15
下一篇 2023-04-15

发表评论

登录后才能评论

评论列表(0条)

保存