首页 > 试题广场 >

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

[编程题]删除emp_no重复的记录,只保留最小的id对应的记录。
  • 热度指数:161519 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
删除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);

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表为(注:最后会select * from 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

输入

drop table if exists titles_test;
CREATE TABLE 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');

输出

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
select id, emp_no, title, from_date, to_date
from (select id, emp_no, title, from_date, to_date, rank() over (partition by emp_no order by id) rk from titles_test) a
where a.rk = 1;
这个为什么不正确呢???求求大佬们解答
发表于 2024-09-06 11:30:51 回复(1)
with t1 as
(
    select
            min(id) min_id
    from
            titles_test
    group by    emp_no
)
delete
from
    titles_test
where
    id not in (
    select min_id from t1
)
发表于 2024-08-06 20:45:52 回复(0)
delete from titles_test
where id in (select id from (select id,row_number() over(partition by emp_no order by id asc) as rn from titles_test) a where rn!=1)
发表于 2024-07-23 16:14:56 回复(0)
create table `titles_id` as (
	select min(`id`)as min_id from `titles_test` 
    group by `emp_no`,`title`,`from_date`,`to_date`);
delete from `titles_test` 
where `id` not in (select `min_id` from `titles_id`);

发表于 2024-07-18 10:19:40 回复(0)
删除和查询不可同时进行,WHERE后取得的应是具体数值/范围,因此多一层套嵌即可。
DELETE FROM titles_test
WHERE id NOT IN (
    SELECT id
    FROM(
        SELECT min(id) AS id
        FROM titles_test
        GROUP BY emp_no
    ) AS t2
);


发表于 2024-04-16 11:30:00 回复(0)
个位大佬,小学生一枚,不懂为什么不行啊,请指教
select min(id), emp_no, title, from_date, to_date
from  titles_test
group by emp_no, title, from_date, to_date

编辑于 2024-02-27 21:27:52 回复(1)
# 需要注意的是,查出来的数据不能直接用作更新条件,需要放在子查询中
	
WITH info AS (
SELECT
    min(tt.id) id
FROM titles_test  tt
GROUP BY emp_no
)
DELETE FROM titles_test t
WHERE NOT EXISTS(
    SELECT 1
    FROM info i
    WHERE i.id = t.id
)


编辑于 2024-02-24 23:28:25 回复(0)
delete from titles_test
where id not in
(select * from (select min(id) from titles_test group by emp_no) temp);

发表于 2024-02-05 12:51:28 回复(0)
窗口函数
with a as (
    select
        emp_no,
        id,
        rank() over(partition by emp_no order by id) as rk
    from titles_test
)
delete from titles_test where
id in (select id from a where rk != 1);

编辑于 2024-01-27 20:09:51 回复(0)
DELETE a
FROM titles_test a,titles_test b
WHERE a.emp_no=b.emp_no AND a.id>b.id


编辑于 2024-01-22 22:45:54 回复(0)
with
    t1 as (
        select
            t.*,
            row_number() over (
                partition by
                    emp_no
                order by
                    id
            ) ranking
        from
            titles_test t
    )
delete from
    titles_test
where
    id in (
        select
            id
        from
            t1
        where
            t1.ranking != 1
    );

编辑于 2024-01-17 09:39:09 回复(0)
delete from titles_test b
where id not in(select * from
    (select min(id) as id from titles_test
    group by emp_no) a 
)
SQL_ERROR_INFO: "You can't specify target table 'titles_test' for update in FROM clause"是因为不能先select出同一表中的某些值,再update这个表(在同一语句中) (MySQL的UPDATE或DELETE中子查询不能为同一张表)
编辑于 2024-01-15 11:38:11 回复(0)
不能先select出同一表中的某些值,再update这个表(在同一语句中)

(MySQL的UPDATE或DELETE中子查询不能为同一张表)

办法:把子查询表添加别名就可以了,如下:

delete from titles_test where id not in (select * from (select min(id) from titles_test
group by emp_no)as a);
发表于 2023-11-14 16:25:45 回复(0)
正向思维:利用窗口函数把要删除的id选择出来直接删除

DELETE FROM titles_test
where id in
(select id from
(select *,row_number()over(partition by emp_no order by emp_no) num
from titles_test ) q
where num != 1)

发表于 2023-08-06 17:54:15 回复(0)
使用内连接,然后删除即可
delete t1
from
    titles_test t1
    inner join titles_test t2 using (emp_no)
where
    t1.id > t2.id


发表于 2023-05-15 11:17:53 回复(0)
一个问题:不能边查边删
所以需要加别名:
delete from titles_test 
where id  not in (
    select * from 
    (
        select min(id)
        from titles_test 
        group by emp_no
    )a 
)

发表于 2023-04-28 17:42:09 回复(0)
delete from titles_test
where id in (select id from (select id, row_number() over (partition by emp_no) as rnk from titles_test) t where rnk > 1);

发表于 2023-01-02 11:33:55 回复(0)
delete t1 
from titles_test t1 inner join titles_test t2
on t1.emp_no = t2.emp_no 
where t1.id > t2.id;

发表于 2022-12-28 23:11:05 回复(0)
with t1 as (
            select 
                id,
                row_number()over(partition by emp_no order by id) as rn
            from 
                titles_test
            )
delete from titles_test 
where id in (select id from t1 where rn <> 1);

-- 用开窗函数标序号,最小的为序号1,则删除时用  where rn <> 1 即可

发表于 2022-12-12 23:22:16 回复(0)