zoukankan      html  css  js  c++  java
  • 删除重复记录sql

    题目描述
    删除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');
    

    思路:
    先查询出重复的emp_no 记录,用group by ...having count(emp_no )>1;
    再用min函数筛选出id最小的值;
    最后用not in 反选出非最小id的重复记录,直接删除

    参考答案:

    delete from titles_test  where id not in(
    select min(id) from titles_test  group by emp_no having count(emp_no)>1 )
    
    做成比做好更重要
  • 相关阅读:
    小程序学习资料
    tomcat单应用多实例部署报错 应用jar不存在
    nginx windows版本 1024限制
    mysql连接数
    rocketmq
    nginx路径匹配
    war包的压缩解压缩
    IIS访问HTTP Error 400. The request hostname is invalid
    Microsoft 安全扫描程序
    vscode
  • 原文地址:https://www.cnblogs.com/fruit1024/p/12310835.html
Copyright © 2011-2022 走看看