2017-05-05 43 views
2

我试图运行该删除查询MYSQL中(通过phpMyAdmin的),我不断收到此错误:2删除联接得到错误 - MySQL的

DELETE 
p,pm 
from wp_posts p 
inner join wp_postmeta pm on pm.post_id = p.id 
where p.id in (SELECT MIN(id) AS min_id 
     FROM wp_posts inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id and meta_key = 'old_id') 
     WHERE post_type = 'post' 
     GROUP BY meta_value 
     HAVING COUNT(*) > 1) 

任何想法,为什么?

回答

1

展望代码
你不应该在子查询中使用IN子句

DELETE p, pm 
from wp_posts as p 
inner join wp_postmeta as pm on pm.post_id = p.id 
where p.id in (SELECT MIN(id) 
     FROM wp_posts 
     inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id 
            and meta_key = 'old_id') 
     WHERE post_type = 'post' 
     GROUP BY meta_value 
     HAVING COUNT(*) > 1) 
+0

我仍然得到:你不能指定目标表“P”的更新在FROM子句 – Adi

+0

答案更新FROM在错误的地方子句 – scaisEdge

+0

'DELETE p,PM 从wp_posts p 内上pm.post_id加入wp_postmeta PM = p.id 其中p.id在(SELECT MIN(ID) FROM wp_posts内上(wp_posts加入wp_postmeta .ID = wp_postmeta.post_id和meta_key ='old_id') WHERE post_type ='post' GROUP BY meta_value HAVING COUNT(*)> 1)'这是我的查询 – Adi