我正在尝试删除除第一个用户之外的某个用户的所有记录,但是我收到此错误:
1093 - 您不能在 FROM 子句中指定目标表“历史记录”进行更新
我不明白问题出在哪里,而且我在 SO 中发现了一个类似的问题,提出了相同的解决方案。这是我的查询:
DELETE FROM `History` WHERE ID = '3' AND Date NOT IN (SELECT min(Date) FROM `History` WHERE ID = '3')
我正在尝试删除除第一个用户之外的某个用户的所有记录,但是我收到此错误:
我不明白问题出在哪里,而且我在 SO 中发现了一个类似的问题,提出了相同的解决方案。这是我的查询:
DELETE FROM `History` WHERE ID = '3' AND Date NOT IN (SELECT min(Date) FROM `History` WHERE ID = '3')
尝试将其转换为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
在执行上述语句之前,请先备份表。
请尝试以下查询一次
DELETE FROM `History` main WHERE main.ID = '3'
AND main.Date NOT IN
(SELECT min(inn.Date) FROM `History` WHERE main.ID = inn.id)
这是解决此问题的另一种方法,通过将子查询包装在子查询的另一层中。MySQL 生成中间查询,以防止此错误:
DELETE FROM `History`
WHERE ID = '3' AND
Date NOT IN (select mindate
from (SELECT min(Date) as mindate FROM `History` WHERE ID = '3')
)