我正在尝试获取最新的10行,并使用一个查询删除特定ID的剩余行。
这是我的问题:
DELETE
FROM `courses`
WHERE customer_id = 11
ORDER BY id ASC LIMIT (SELECT COUNT(*) FROM `courses` WHERE customer_id = 11);
请有人帮我识别此查询中的错误。
感谢。
答案 0 :(得分:1)
我找到了解决方案,这是查询:
$sql1="DELETE FROM `courses` WHERE customer_id =11 AND id NOT IN (SELECT id
FROM (SELECT id FROM `courses` WHERE customer_id =11 ORDER BY id DESC LIMIT 10) AS foo)";
$conn->query($sql1);
通过上面的查询,我们得到最新的10行并删除特定id的剩余行。