我正在使用WordPress。我想从wp_post
表中删除重复的帖子。确定帖子是否重复的条件是基于两个不同的元键是否具有相同的元值。
我遇到了此查询,以删除具有单个元值重复的帖子:
DELETE p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_email'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
我想要实现的是这样的:
DELETE p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_email'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
AND pm1.meta_key = '_list'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
我尝试运行以下内容:
DELETE p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_email'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
AND p.ID IN (
SELECT p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_list'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
)
,但是在MySQL上收到错误消息。我在做什么错了?
任何反馈表示赞赏!谢谢!
答案 0 :(得分:1)
查看您的代码..您不能使用meta_key ='_email'AND ='_list'可能是您的意思是meta_key ='_email'OR ='_list' 您不应该(更多)基于何处使用旧的隐式联接sintax,而应使用更具可读性的显式JOIN sintax
DELETE p, pm1
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm1 ON p.ID = pm1.post_id
AND ( pm1.meta_key = '_email' OR pm1.meta_key = '_list' )
INNER JOIN {$wpdb->postmeta} pm2 ON pm1.post_id > pm2.post_id
AND pm1.meta_value = pm2.meta_value
AND pm1.meta_key = pm2.meta_key
无论如何,如果您查询的是wromg子查询,请选择试试p.ID而不是p,pm1
DELETE p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_email'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
AND p.ID IN (
SELECT p.ID
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_list'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
)
它也可能表明您不能从这种情况下从用作选择的表中删除 在这种情况下,请尝试将内部结果强制为带有嵌套子查询的临时表
DELETE p, pm1
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_email'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
AND p.ID IN (
select t.ID from (
SELECT p.ID
FROM
{$wpdb->posts} p,
{$wpdb->postmeta} pm1,
{$wpdb->postmeta} pm2
WHERE
p.ID = pm1.post_id
AND pm1.post_id > pm2.post_id
AND pm1.meta_key = '_list'
AND pm1.meta_key = pm2.meta_key
AND pm1.meta_value = pm2.meta_value
) t
)