php+mysql怎么删除重复的记录只保留一个最新的记录?

php+mysql怎么删除重复的记录只保留一个最新的记录?,第1张

换个思路, 你再新建一个表,表里的字段跟你这个表的字段一样。。

然后 insert into select from 。。。。这里你应该懂了吧,, 就是去重查询之后,然后将结果集插入到指定的表中。。

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)


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存