sql查询的问题

时间:2015-11-05 01:44:40

标签: php mysql wordpress

我正在将旧的代码点火器应用程序迁移到WordPress。我已将所有数据导入为自定义帖子(product),但问题出现在旧网站上,他们重复使用页面slu g,当WP拉取帖子时会导致一些奇怪的问题(因为WordPress要求它是唯一的默认)。所以,我想弄清楚的是我如何编写一个查询或脚本来查找所有重复的帖子名称(来自wp_posts)并添加' -2&#39 ;,' -3',' -4'等所有重复项。我的另一个选择是尝试修复WordPress,所以我不需要独特的页面slu ,,但我甚至不知道我会在哪里转向(尽管如果可能的话,这将是最好的结果!!)。

如果我跑

SELECT * FROM wp_posts WHERE post_type = 'product'

它返回5973结果。运行

SELECT DISTINCT post_title FROM wp_posts WHERE post_type = 'product'

它返回1800行,因此手动执行此操作将花费大量时间。

我试图在php中编写一个脚本来输出每个单独的查询,但是我的sql语法中有一个错误,最终导致每组命令中的第一个应用于每个匹配结果而不是第一个结果。有什么想法吗?

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>';
}

以下是此输出的示例:

UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-1" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-2" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-3" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-4" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-5" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-6" WHERE post_name = "headlight-assembly-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-left-7" WHERE post_name = "headlight-assembly-left";

UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-1" WHERE post_name = "headlight-assembly-right";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-2" WHERE post_name = "headlight-assembly-right";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-3" WHERE post_name = "headlight-assembly-right";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-4" WHERE post_name = "headlight-assembly-right";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-5" WHERE post_name = "headlight-assembly-right";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-assembly-right-6" WHERE post_name = "headlight-assembly-right";

UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-1" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-2" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-3" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-4" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-5" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-6" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-7" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-8" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-9" WHERE post_name = "headlight-door-surround-left";
UPDATE wp_o1y4a6ifud_posts SET post_name = "headlight-door-surround-left-10" WHERE post_name = "headlight-door-surround-left";

我的查询有什么问题吗?

2 个答案:

答案 0 :(得分:1)

我在当天用Excel做了很多。但是,如果有1M行甚至只有10K,那该怎么办呢。

在测试环境中执行此操作。无论如何,它都会对您产生影响。但这就是我要做的。

除了this以外我对WordPress一无所知。

所以我假设它的id是一个int auto_increment,因此它必须是主键。

模式

create table wp_posts
(   id int auto_increment primary key,
    post_title varchar(1000) not null,
    post_type varchar(50) not null
);
insert wp_posts (post_title,post_type) values 
('a','product'),('a','service'),('d','product'),
('b','product'),('c','product'),('b','product'),('b','product'),('d','product'),('z','fancy');

查看数据

SELECT id, post_title 
FROM wp_posts 
WHERE post_type = 'product' 
order by post_title,id; -- give some weight to id, what the heck
+----+------------+
| id | post_title |
+----+------------+
|  1 | a          |
|  4 | b          |
|  6 | b          |
|  7 | b          |
|  5 | c          |
|  3 | d          |
|  8 | d          |
+----+------------+

显示代表重复标题的标题(针对产品)

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 |
+----+------------+-----------+------------+----------+

- 在上面添加一个行号列,从每个分组的标题

开始递增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)。< / p>

但是,让我们在一个更新声明中完成所有操作并快速完成。

这需要抓取最后一块代码并从中生成另一个派生表。

&#34;更新加入&#34;图案

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     |
+----+------------+-----------+

&#34; a&#34;的后期标题被留下来作为一个边缘条件供你思考,因为它是第二个服务。

以下是Advanced MySQL user variable techniques的参考资料。看到的交叉连接只是在开始时初始化这些变量。没有更多,没有更少。由于输出经历了一组新的标题,它只是将rownumber设置为1.它可能看起来很神秘。好吧,它看起来很神秘。

祝你好运。哦,在测试环境中这样做。

答案 1 :(得分:1)

因为帖子有重复的名称,并且您正在使用的逻辑会生成一个查询,该查询会使用“A-1”更新标题“A”的所有帖子。

您需要使用标题以外的字段定位帖子。尝试抓取帖子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>';
}