网站最近发现好多文章有重复的,手工一个一个很麻烦,就想到了程序。下面就是WordPress删除重复标题文章的几种方法
以下为代码解释:
create table tmp as select min(主键) as col1 from 去重表名 GROUP BY 去重字段;
delete from 去重表名 where 主键 not in (select col1 from tmp);
drop table tmp;
一、通过操作SQL数据库删除:
去除重复文章,只保留一篇
CREATE TABLE my_tmp AS SELECT MIN(ID) AS col1 FROM wp_posts GROUP BY post_title;
DELETE FROM wp_posts WHERE ID NOT IN (SELECT col1 FROM my_tmp);
DROP TABLE my_tmp;
去除重复文章,一片都不保留
CREATE TABLE my_tmp AS Select ID AS col1 From wp_posts Where post_title In (Select post_title From wp_posts Group By post_title Having Count(*)>2);
DELETE FROM wp_posts WHERE ID IN (SELECT col1 FROM my_tmp);
DROP TABLE my_tmp;
另一种去除所有重复文章的方法
CREATE TABLE my_tmp AS Select ID AS col1 From wp_posts Where post_title In (Select post_title From wp_posts Group By post_title Having Count(*)>2);DELETE FROM wp_posts WHERE ID IN (SELECT col1 FROM my_tmp); DROP TABLE my_tmp;
操作方法很简单,只需将上面的SQL语句,放到自己网站数据库的 SQL框里,然后执行就可以了。(注意:操作之前,请先进行网站备份)
二、php程序删除
require(‘./wp-load.php’);
$strsql=”create table my_tmp as select min(ID) as col1 from cd_posts group by post_title”;
$strsql1=”delete from cd_posts where ID not in (select col1 from my_tmp)”;
$strsql2=”drop table my_tmp”;
$result=mysql_query($strsql);
$result=mysql_query($strsql1);
$result=mysql_query($strsql2);
保存以上代码以为delete.php,放在根目录,想要删除重复文章的时候,访问这个文件就可以了。
1
个人实测,方法一里面的第1个完美解决问题。
如何sql出现#1050 – Table ‘my_tmp’ already exists 书名他在运行,等他结束。。别急
转载请注明:范耀祖 » wordpress重复文章如何快速删除?(去重保留)