最初使用
DELETE
FROM
consu
WHERE
id IN (
SELECT
id
FROM
consu
GROUP BY
order_no
HAVING
count(*) > 1
);
报错You can't specify target table 'consume' for update in FROM clause
不能先select出同一表中的某些值,再update这个表(在同一语句中)
DELETE
FROM
consu
WHERE
id IN (
select id from (
SELECT
id
FROM
consu
GROUP BY
order
HAVING
count(*) > 1) m
);
用这种就可以了 也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。
删除表中多余的重复记录(多个字段),只留有rowid最小的记录
DELETEFROM
vitae a
WHERE
(a.peopleId, a.seq) IN (
SELECT
peopleId,
seq
FROM
vitae
GROUP BY
peopleId,
seq
HAVING
count(*) > 1
)
AND rowid NOT IN (
SELECT
min(rowid)
FROM
vitae
GROUP BY
peopleId,
seq
HAVING
count(*) > 1
)