谜语? MYSQL - 使用 JOIN 在子查询中限制。想要删除除每个类别至少 100 个帖子之外的所有帖子,但要删除几个类别中的一些帖子

riddle? MYSQL - LIMIT in subquery using JOIN. Want to delete all posts except minimum 100 posts per category but some posts in several categories

你好,使用 mysql8 (8.0.23) 的 wordpress:

我想删除所有 post,每个类别至少要删除 100 post。 所以在前端,我希望点击的任何类别至少显示 100 posts.

这看起来很简单,除了许多 post 存在于不止一个类别中。

所以对于类别 A,如果我删除所有 post>100,post 行号 112 也可能存在于类别 B 中,但只是 post 行 80,并且因此被删除。

看到谜语了吗?

所以在一次测试中使用了 4 个类别,我怎样才能

对于下面的示例,类别 A、B、C、D 的 ID (71,72,73,74) 在子查询中使用了 LIMIT(必须使用 JOIN,因为在子查询中不允许使用 LIMIT? )


SELECT * FROM wp_posts
 
    JOIN 
    (
        SELECT ID 
        FROM wp_posts 
        WHERE NOT 
        (wp_posts.ID IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (72) ) ) 
        LIMIT 100
    ) d
    ON wp_posts.ID 
    IN (d.ID)
    
    JOIN 
       (
        SELECT ID 
        FROM wp_posts 
        WHERE NOT 
        (wp_posts.ID IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (73) ) ) 
         LIMIT 100
         ) e
        ON wp_posts.ID 
        IN (e.ID)
   
    JOIN 
       (
        SELECT ID 
        FROM wp_posts 
        WHERE NOT 
        (wp_posts.ID IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (74) ) ) 
         LIMIT 100
         ) f
        ON wp_posts.ID 
        IN (f.ID)

 
  WHERE 
        (wp_posts.ID IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (71) ) ) 
        AND (wp_posts.post_type = 'post' ) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC
LIMIT 99999999
OFFSET 100

结果为零行

感谢帮助!!

解决了..但没有将 LIMIT 与 JOIN 结合使用。

感谢@nbk 的评论,我正在使用 ROW_NUMBER() OVER。这在子查询中是允许的。我在这里将其作为“select”语句而不是删除(用于测试)。

此处使用类别 ID 为 71、72、73 和 74 的四个类别。

SELECT wp_posts.*
FROM  wp_posts 
   
WHERE 

(wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish')   
   
AND

-- posts discluded if under 100 most recent per category:
(

( wp_posts.ID NOT IN (SELECT ID FROM (SELECT ID, ROW_NUMBER() OVER (ORDER BY wp_posts.post_date DESC) AS rownumber
  FROM wp_posts
  WHERE (wp_posts.ID IN (SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (71) ) ) 
AND (wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish')  
) AS foo
WHERE rownumber <= 100
) 
)

AND

( wp_posts.ID NOT IN (SELECT ID FROM (SELECT ID, ROW_NUMBER() OVER (ORDER BY wp_posts.post_date DESC) AS rownumber
  FROM wp_posts
  WHERE (wp_posts.ID IN (SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (72) ) ) 
AND (wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish')  
) AS foo
WHERE rownumber <= 100
) 
)

AND

( wp_posts.ID NOT IN (SELECT ID FROM (SELECT ID, ROW_NUMBER() OVER (ORDER BY wp_posts.post_date DESC) AS rownumber
  FROM wp_posts
  WHERE (wp_posts.ID IN (SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (73) ) ) 
AND (wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish')  
) AS foo
WHERE rownumber <= 100
) 
)

AND

( wp_posts.ID NOT IN (SELECT ID FROM (SELECT ID, ROW_NUMBER() OVER (ORDER BY wp_posts.post_date DESC) AS rownumber
  FROM wp_posts
  WHERE (wp_posts.ID IN (SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (74) ) ) 
AND (wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish')  
) AS foo
WHERE rownumber <= 100
) 
)

)