简体   繁体   中英

how to use the “DELETE” in mysql with COUNT and INNER JOIN?

I'm starting in mysql, and I wonder how can I do to remove the result of the SELECT below, thank you all!

SELECT ID,post_name,meta_value, guid, COUNT( meta_value ) 
FROM wp_postmeta
INNER JOIN wp_posts
WHERE wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_value >100 
GROUP BY meta_value
HAVING COUNT( meta_value ) >1

You can first select and create a array or you can directly put this query with delete query using IN clause.

 DELETE from wp_posts
 WHERE ID IN (
 SELECT ID
 FROM wp_posts
 INNER JOIN wp_postmeta
 WHERE wp_postmeta.post_id = wp_posts.ID AND  wp_postmeta.meta_value >100 
 GROUP BY meta_value
 HAVING COUNT( meta_value ) >1)

If you set foreign key with postmeta colum post_id as on delete cascade then data from this table will automatically remove.

Otherwise please consider to create unique array of post ids and postmeta ids. And use two separate delete query with IN clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM