我正在尝试删除除第一个用户以外的某个用户的所有记录,但是我收到此错误:
我不明白是什么问题,我在SO中发现了一个类似的问题,提出了同样的解决方案。这是我的问题:
DELETE FROM `History` WHERE ID = '3' AND Date NOT IN (SELECT min(Date) FROM `History` WHERE ID = '3')
答案 0 :(得分:1)
尝试将其转换为JOIN
DELETE a
FROM History a
LEFT JOIN History b
ON a.Date = b.Date AND
b.ID = 3
WHERE b.date IS NULL AND
a.ID = 3
在执行上述声明之前,请先备份表格。
答案 1 :(得分:0)
请尝试以下查询
DELETE FROM `History` main WHERE main.ID = '3'
AND main.Date NOT IN
(SELECT min(inn.Date) FROM `History` WHERE main.ID = inn.id)
答案 2 :(得分:0)
这是通过将子查询包装在子查询的另一层中来解决此问题的另一种方法。 MySQL生成中间查询,以防止出现此错误:
DELETE FROM `History`
WHERE ID = '3' AND
Date NOT IN (select mindate
from (SELECT min(Date) as mindate FROM `History` WHERE ID = '3')
)