问题描述
删除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);
Sql语句
1、先按emp_no分组,找出每组最小的id
2、删除id中不等于上一步的id
delete
from titles_test
where id not in (select min(id)
from titles_test
group by emp_no);