一:首先是这么想的
DELETE
FROM
t_4g_phone
WHERE
id NOT IN (
SELECT
max(b.id) AS id
FROM
t_4g_phone b
GROUP BY
b.SERIAL_NUMBER
)
发现在mysql中会报错
错误信息:[Err] 1093 – You can’t specify target table ‘t_4g_phone’ for update in FROM clause
于是查资料说是不能先select出同一表中的某些值,再update这个表(在同一语句中)
二:正确写法
DELETE
FROM
t_4g_phone
WHERE
id NOT IN (
SELECT
id
FROM
(
SELECT
max(b.id) AS id
FROM
t_4g_phone b
GROUP BY
b.SERIAL_NUMBER
) b
)
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题