今天写了一个sql语句,功能是删除一个表中指定字段有重复的数据:
DELETE FROM test WHERE id IN (SELECT id FROM test GROUP BY id HAVING COUNT(id) > 1)
提示错误:
You can't specify target table 'test' for update in FROM clause
网上查了下原因:mysql中不能先select出同一表中的某些值,再update这个表(在同一语句中)
其实小小的修改一下就能实现这样的功能,修改后sql语句:
DELETE FROM test WHERE id IN (SELECT a.id FROM (SELECT id FROM test GROUP BY id HAVING COUNT(id) > 1) AS a)
DELETE FROM test WHERE id IN (SELECT id FROM test GROUP BY id HAVING COUNT(id) > 1)
提示错误:
You can't specify target table 'test' for update in FROM clause
网上查了下原因:mysql中不能先select出同一表中的某些值,再update这个表(在同一语句中)
其实小小的修改一下就能实现这样的功能,修改后sql语句:
DELETE FROM test WHERE id IN (SELECT a.id FROM (SELECT id FROM test GROUP BY id HAVING COUNT(id) > 1) AS a)