3 月 292015
 

查看重复帖子

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)

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny