查看重复帖子
select subject from pw_bbs_threads group by subject having count(subject) > 1;
删除帖子,但是实际操作的时候,mysql版本不支持这种写法。
delete from pw_bbs_threads where subject in (select subject from pw_bbs_threads group by subject having count(subject) > 1) and tid not in (select min(tid) from pw_bbs_threads group by subject having count(subject) >1);
查出重复帖子的tid
select tid from pw_bbs_threads where subject in (select subject from pw_bbs_threads group by subject having count(subject) > 1) and tid not in (select min(tid) from pw_bbs_threads group by subject having count(subject) >1);
实际删除重复帖子的办法:
create table tmp as select tid from pw_bbs_threads where subject in (select subject from pw_bbs_threads group by subject having count(subject) > 1) and tid not in (select min(tid) from pw_bbs_threads group by subject having count(subject) >1);
delete from pw_bbs_threads where tid in (select tid from tmp);
drop table tmp;
后续的补充,其实上面只是删除了标题 还需要删除内容pw_bbs_threads_content表的数据
create table tmp as select tid from pw_bbs_threads ;
delete from pw_bbs_threads_content where tid not in (select tid from tmp);
drop table tmp;
上面的其实可以执行成
create table tmp as select tid from pw_bbs_threads where subject in (select subject from pw_bbs_threads group by subject having count(subject) > 1) and tid not in (select min(tid) from pw_bbs_threads group by subject having count(subject) >1);
delete from pw_bbs_threads where tid in (select tid from tmp);
delete from pw_bbs_threads_content where tid in (select tid from tmp);
drop table tmp;
参考是:http://blog.csdn.net/softwave/article/details/3890576
MySQL查询及删除重复记录的方法
(一)
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)