题解 | #删除emp_no重复的记录,只保留最小的id对应的记录。#
删除emp_no重复的记录,只保留最小的id对应的记录。
http://www.nowcoder.com/practice/3d92551a6f6d4f1ebde272d20872cf05
分析:
在删除后边出现要删除表的表名会报错,[HY000][1093] You can't specify target table 'titles_test' for update in FROM clause
可以将不需要删除的数据放入临时表中,利用with用法。。。。。
- with用法不仅可以select数据,还可以删除删除
- sql删除数据的语法:delete from 表 where 条件;
代码:
方法1:with写法: with linshi as ( select min(id) from titles_test GROUP by emp_no ) delete from titles_test where id not in (select * from linshi);
方法2:将子查询当作表 delete from titles_test where id not in ( select * from ( select min(id) from titles_test GROUP by emp_no ) as a );