Php sql查询的问题
我正在将一个旧的CodeIgniter应用程序迁移到WordPress。我已经将所有的数据作为自定义帖子产品导入,但问题是在旧站点上,它们重复使用页面段塞,当WP拉取帖子时会导致一些奇怪的问题,因为WordPress默认要求它是唯一的。所以,我想弄明白的是,我如何编写一个查询或脚本,从wp_帖子中查找所有重复的帖子名称,并为所有重复的帖子添加'-2'、'-3'、'-4'等。我的另一个选择是尝试修复WordPress,这样我就不需要独特的页面塞,但我甚至不知道我会转向哪里,尽管,如果可能的话,这将是最好的结果 如果我跑 从wp_posts中选择*,其中post_type='product' 它返回5973个结果。运行 从wp_帖子中选择不同的帖子标题,其中帖子类型='产品' 它返回1800行,因此手动执行此操作将花费大量时间 我试图用php编写一个脚本来输出每个查询,但我的sql语法中有一个错误,最终导致每组命令中的第一个应用于每个匹配结果,而不是第一个结果。有什么想法吗Php sql查询的问题,php,mysql,wordpress,Php,Mysql,Wordpress,我正在将一个旧的CodeIgniter应用程序迁移到WordPress。我已经将所有的数据作为自定义帖子产品导入,但问题是在旧站点上,它们重复使用页面段塞,当WP拉取帖子时会导致一些奇怪的问题,因为WordPress默认要求它是唯一的。所以,我想弄明白的是,我如何编写一个查询或脚本,从wp_帖子中查找所有重复的帖子名称,并为所有重复的帖子添加'-2'、'-3'、'-4'等。我的另一个选择是尝试修复WordPress,这样我就不需要独特的页面塞,但我甚至不知道我会转向哪里,尽管,如果可能的话,这将
global $wpdb;
$distinct_post_names = $wpdb->get_results( 'SELECT DISTINCT post_name FROM ' . $wpdb->prefix . 'posts WHERE post_type = "product" AND post_name != ""');
foreach( $distinct_post_names as $distinct_post ) {
$these_posts = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'posts WHERE post_name = "' . $distinct_post->post_name .'" AND post_type="product"');
for ( $i = 0; $i < count($these_posts); $i++ ) {
if ( $i != 0) {
echo 'UPDATE ' . $wpdb->prefix . 'posts SET post_name = "' . $distinct_post->post_name . '-' . $i . '" WHERE post_name = "' . $distinct_post->post_name . '";<br>';
}
}
echo '<br>';
}
你知道我的查询有什么问题吗?我以前用Excel做过很多次。但如果有100万行,甚至只有10万行呢 在测试环境中执行此操作。不管发生什么事,都是你的责任。但我会这么做 我对WordPress一无所知,只知道我只是查了一下 所以我假设它有一个id,它是一个int自动增量,因此它必须是主键 模式 偷看数据 显示表示产品重复标题的标题 在联接中从上面生成一个派生表,以将输出限制为仅重复的标题。 -在上面添加一个行号列,每个标题分组从1开始递增
select w.*,
@rn:=if(w.post_title=@grp,@rn+1,1) as rownum,
@grp:=coalesce(null,w.post_title) as theGrp
from wp_posts w
join
( SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1
) xxx -- this is the alias, every derived table needs an alias
on w.post_title=xxx.post_title
cross join (select @rn:=0,@grp='') params;
+----+------------+-----------+--------+--------+
| id | post_title | post_type | rownum | theGrp |
+----+------------+-----------+--------+--------+
| 4 | b | product | 1 | b |
| 6 | b | product | 2 | b |
| 7 | b | product | 3 | b |
| 3 | d | product | 1 | d |
| 8 | d | product | 2 | d |
+----+------------+-----------+--------+--------+
这可能足以让您在关注输出的情况下做您想做的事情,并且对上面在其他sql生成器中使用的例程进行了调整,并使用了PK id
但是,让我们在一个update语句中完成这一切,然后用fast结束它
这需要抓取最后一段代码,并从中生成另一个派生表
以及使用连接模式的更新
update wp_posts
join
( select w.*,
@rn:=if(w.post_title=@grp,@rn+1,1) as rownum,
@grp:=coalesce(null,w.post_title) as theGrp
from wp_posts w
join
( SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1
) xxx -- this is the alias, every derived table needs an alias
on w.post_title=xxx.post_title
cross join (select @rn:=0,@grp='') params
) xyz -- every derived table needs an alias
on xyz.id=wp_posts.id
set wp_posts.post_title=concat(xyz.post_title,'-',cast(xyz.rownum as char(5)));
-- 5 rows(s) affected
后果
a的post_标题是留给您思考的一个边缘条件,因为它是为第二个服务的
这里有一个参考资料。看到的交叉连接只是在开始时初始化这些变量。不多不少。当输出经历一组新的标题时,它仅将行数设置回1。它可能看起来很神秘。好吧,看起来确实很神秘
祝你好运。哦,在测试环境中执行此操作。因为帖子有重复的名称,并且您使用的逻辑生成一个查询,该查询将标题为“a”的所有帖子更新为“a-1” 您需要使用标题以外的字段来定位帖子。尝试获取帖子ID。因此,这将是:
global $wpdb;
$distinct_post_names = $wpdb->get_results( 'SELECT DISTINCT post_name FROM ' . $wpdb->prefix . 'posts WHERE post_type = "product" AND post_name != ""');
foreach( $distinct_post_names as $distinct_post ) {
$these_posts = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'posts WHERE post_name = "' . $distinct_post->post_name .'" AND post_type="product"');
for ( $i = 0; $i < count($these_posts); $i++ ) {
if ( $i != 0) {
echo 'UPDATE ' . $wpdb->prefix . 'posts SET post_name = "' . $distinct_post->post_name . '-' . $i . '" WHERE ID = "' . $these_posts[$i]->ID . '";<br>';
}
}
echo '<br>';
}
哇,多么彻底的帖子!我将通读这篇文章,如果我有任何问题,我会发表评论,但谢谢!
SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1;
+------------+----------+
| post_title | theCount |
+------------+----------+
| b | 3 |
| d | 2 |
+------------+----------+
select *
from wp_posts w
join
( SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1
) xxx -- this is the alias, every derived table needs an alias
on w.post_title=xxx.post_title;
+----+------------+-----------+------------+----------+
| id | post_title | post_type | post_title | theCount |
+----+------------+-----------+------------+----------+
| 4 | b | product | b | 3 |
| 6 | b | product | b | 3 |
| 7 | b | product | b | 3 |
| 3 | d | product | d | 2 |
| 8 | d | product | d | 2 |
+----+------------+-----------+------------+----------+
select w.*,
@rn:=if(w.post_title=@grp,@rn+1,1) as rownum,
@grp:=coalesce(null,w.post_title) as theGrp
from wp_posts w
join
( SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1
) xxx -- this is the alias, every derived table needs an alias
on w.post_title=xxx.post_title
cross join (select @rn:=0,@grp='') params;
+----+------------+-----------+--------+--------+
| id | post_title | post_type | rownum | theGrp |
+----+------------+-----------+--------+--------+
| 4 | b | product | 1 | b |
| 6 | b | product | 2 | b |
| 7 | b | product | 3 | b |
| 3 | d | product | 1 | d |
| 8 | d | product | 2 | d |
+----+------------+-----------+--------+--------+
update wp_posts
join
( select w.*,
@rn:=if(w.post_title=@grp,@rn+1,1) as rownum,
@grp:=coalesce(null,w.post_title) as theGrp
from wp_posts w
join
( SELECT post_title,count(*) as theCount
FROM wp_posts
WHERE post_type = 'product'
group by post_title
having theCount>1
) xxx -- this is the alias, every derived table needs an alias
on w.post_title=xxx.post_title
cross join (select @rn:=0,@grp='') params
) xyz -- every derived table needs an alias
on xyz.id=wp_posts.id
set wp_posts.post_title=concat(xyz.post_title,'-',cast(xyz.rownum as char(5)));
-- 5 rows(s) affected
select * from wp_posts;
+----+------------+-----------+
| id | post_title | post_type |
+----+------------+-----------+
| 1 | a | product |
| 2 | a | service |
| 3 | d-1 | product |
| 4 | b-1 | product |
| 5 | c | product |
| 6 | b-2 | product |
| 7 | b-3 | product |
| 8 | d-2 | product |
| 9 | z | fancy |
+----+------------+-----------+
global $wpdb;
$distinct_post_names = $wpdb->get_results( 'SELECT DISTINCT post_name FROM ' . $wpdb->prefix . 'posts WHERE post_type = "product" AND post_name != ""');
foreach( $distinct_post_names as $distinct_post ) {
$these_posts = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'posts WHERE post_name = "' . $distinct_post->post_name .'" AND post_type="product"');
for ( $i = 0; $i < count($these_posts); $i++ ) {
if ( $i != 0) {
echo 'UPDATE ' . $wpdb->prefix . 'posts SET post_name = "' . $distinct_post->post_name . '-' . $i . '" WHERE ID = "' . $these_posts[$i]->ID . '";<br>';
}
}
echo '<br>';
}