Title: Complex custom query?
Last modified: January 18, 2025

---

# Complex custom query?

 *  Resolved [More Than God](https://wordpress.org/support/users/more-than-god/)
 * (@more-than-god)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/complex-custom-query/)
 * Good day, intending to make query for purging old revisions, but getting error
   on line #27, any ideas to fix?
 *     ```wp-block-code
       DELETE pm FROM wp_postmeta pm WHERE pm.post_id IN (    SELECT p.ID    FROM wp_posts p    WHERE p.post_type = 'revision'        AND p.ID NOT IN (            SELECT recent_revisions.ID            FROM (                    SELECT ID                    FROM wp_posts                    WHERE post_type = 'revision'                    ORDER BY post_parent,                        post_date DESC                ) AS recent_revisions            WHERE recent_revisions.ID IN (                    SELECT ID                    FROM (                            SELECT ID                            FROM wp_posts                            WHERE post_type = 'revision'                            GROUP BY post_parent                            HAVING COUNT(ID) > 15                        ) AS grouped_revisions                )        ));DELETE p FROM wp_posts p WHERE p.post_type = 'revision' AND p.ID NOT IN (    SELECT recent_revisions.ID    FROM (            SELECT ID            FROM wp_posts            WHERE post_type = 'revision'            ORDER BY post_parent,                post_date DESC        ) AS recent_revisions    WHERE recent_revisions.ID IN (            SELECT ID            FROM (                    SELECT ID                    FROM wp_posts                    WHERE post_type = 'revision'                    GROUP BY post_parent                    HAVING COUNT(ID) > 15                ) AS grouped_revisions        ));
       ```
   

Viewing 1 replies (of 1 total)

 *  Plugin Support [Val Meow](https://wordpress.org/support/users/valwa/)
 * (@valwa)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/complex-custom-query/#post-18252340)
 * Hey [@more-than-god](https://wordpress.org/support/users/more-than-god/)! 👋
 * SQL errors depend on your database service and version, so it can be challenging
   to identify the issue at a glance. Since this isn’t directly related to the plugin,
   I’m not sure how much help I can provide in this regard. Always ensure you back
   up your server before performing any operations with the plugin. Thanks!

Viewing 1 replies (of 1 total)

The topic ‘Complex custom query?’ is closed to new replies.

 * ![](https://ps.w.org/database-cleaner/assets/icon-256x256.png?rev=2791575)
 * [Database Cleaner](https://wordpress.org/plugins/database-cleaner/)
 * [Support Threads](https://wordpress.org/support/plugin/database-cleaner/)
 * [Active Topics](https://wordpress.org/support/plugin/database-cleaner/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/database-cleaner/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/database-cleaner/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Val Meow](https://wordpress.org/support/users/valwa/)
 * Last activity: [1 year, 4 months ago](https://wordpress.org/support/topic/complex-custom-query/#post-18252340)
 * Status: resolved