答案 0 :(得分:1)
尝试使用ORDER BY message_id DESC
更改它,因为它会删除所有除了选择的50个条目之外的所有内容,我为查询设置了别名,因为你不能使用相同的表来选择删除操作
DELETE FROM `chat_history` WHERE id NOT IN ( SELECT t.id FROM
(SELECT id FROM chat_history ORDER BY message_id DESC LIMIT 50 ) t)
答案 1 :(得分:1)
E.g ..
DELETE a
FROM chat_history a
LEFT
JOIN
( SELECT x.message_id
FROM chat_history x
JOIN chat_history y
ON y.message_id >= x.message_id
GROUP
BY x.message_id
HAVING COUNT(*) <= 50
) b
ON b.message_id = a.message_id
WHERE b.message_id IS NULL;