删除包含使用join语句的查询的行

时间:2017-06-22 16:14:27

标签: mysql sql sql-delete

我有一个选择查询重复的查询。

select ID, post_id, post_name, post_date, Count(meta_value) FROM wp_yepf_posts p join wp_yepf_postmeta pm on p.ID = pm.post_id WHERE p.post_type = 'product' and pm.meta_key='_sku'  group by meta_value having count(meta_value)>1 

这会返回2458条记录,其中count(meta_value)大于1--我需要删除这些记录(如果可能的话,留下一条记录副本)但每次我尝试删除这样的查询时都会收到错误

delete p 
FROM wp_yepf_posts p 
join wp_yepf_postmeta pm on p.ID = pm.post_id (select DUPLICATE.entity_id in (Select DISTINCT(pm.meta_key='_sku') as sku, Count(pm.meta_key='_sku') as skuCount, entity_id
from wp_yepf_posts p join wp_yepf_postmeta pm on p.ID = pm.post_id 
group by sku Having skucount>1) as dup)

错误是

  

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在'select DUPLICATE.entity_id(选择DISTINCT(pm.meta_key ='_ sku')附近使用正确的语法作为sku,在第1行计算'

1 个答案:

答案 0 :(得分:0)

我认为你错过了那里的运营商:

删除p 来自wp_yepf_posts p 在p.ID = pm.post_id AND 上加入wp_yepf_postmeta pm(选择(选择DISTINCT(pm.meta_key ='_ sku')中的DUPLICATE.entity_id为sku,Count(pm.meta_key ='_ sku') as skuCount,entity_id 来自wp_yepf_posts p在p.ID = pm.post_id上加入wp_yepf_postmeta pm sku将skucount> 1)作为dup)