gpt4 book ai didi

mysql - 随机删除未知数量的记录

转载 作者:行者123 更新时间:2023-11-29 04:49:52 25 4
gpt4 key购买 nike

我有一个表,名为items
结构是: {name, id, category_id}

表中有未知数量的记录,我需要将每个类别的记录数量限制为 MAX 数量,例如 20。

DELETE FROM items
WHERE (SELECT COUNT(category_id) FROM items)> 20
LIMIT (SELECT COUNT(category_id) FROM items) - 20

最佳答案

DELETE FROM items 
WHERE (category_id,id) not in (
SELECT category_id, id FROM items a WHERE id IN (
SELECT id FROM items s
WHERE a.category_id = s.category_id
ORDER BY category_id LIMIT 20)
ORDER BY id)

关于mysql - 随机删除未知数量的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13118466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com