DELETE FROM student WHERE id NOTIN ( SELECT id FROM ( SELECTMIN( id ) AS id FROM student GROUPBY `name` ) tmp)
要多加一层 tmp 包装,否则会遇到:1093 - You can't specify target table 'student' for update in FROM clause
备份表
-- 创建同结构备份表 create table zzz_my_table_220727 like my_table; -- 将需要数据写入备份表 insert into zzz_my_table_220727 select*from my_table ORDERBY id desc LIMIT 1000; -- 情况原表 truncatetable my_table;
SQL AND OR 执行优先级
select id from table01 where condition1 or condition2 and condition3;
-- 等价于: select id from table01 where condition1 or (condition2 and condition3); -- 而非: select id from table01 where (condition1 or condition2) and condition3;