SQL42 删除emp_no重复的记录

本文最后更新于:2022年4月9日 中午


题目描述

删除emp_no重复的记录,只保留最小的id对应的记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);

insert into titles_test values ('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');

删除后titles_test表为

id emp_no title from_date to_date
1 10001 Senior Engineer 1986-06-26 9999-01-01
2 10002 Staff 1996-08-03 9999-01-01
3 10003 Senior Engineer 1995-12-03 9999-01-01
4 10004 Senior Engineer 1995-12-03 9999-01-01

输入描述

输出描述

题解

1
2
delete from titles_test where id not in 
(select min(id) from titles_test group by emp_no) as a;

备注:

  1. 先用 GROUP BY 和 MIN() 选出每个 emp_no 分组中最小的 id,然后用 DELETE FROM
    … WHERE … NOT IN … 语句删除 “非每个分组最小id对应的所有记录”

  2. 在MySQL中,子查询的查询结果必须要指定别名。sqlite则没有要求。