题目描述
删除emp_no重复的记录,只保留最小的id对应的记录。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
解答:
delete from titles_test where id not in
(select min(id) from titles_test group by emp_no)
先用 group by 和 min() 选出每个 emp_no 分组中最小的 id,然后用 delete from … where … not in … 语句删除 “非每个分组最小id对应的所有记录”
delete from titles_test
where emp_no in (select tt.emp_no
from titles_test tt
where tt.emp_no = titles_test.emp_no
and tt.id > titles_test.id);

507

被折叠的 条评论
为什么被折叠?



