欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

196. 删除重复的电子邮箱

程序员文章站 2022-07-08 11:28:55
...
CREATE DATABASE IF NOT EXISTS sql_practice;
USE sql_practice;

182. 查找重复的电子邮箱

https://leetcode-cn.com/problems/duplicate-emails/ 

DROP TABLE IF EXISTS `Person`;
CREATE TABLE `Person`
(
    `Id`    bigint(20)   NOT NULL AUTO_INCREMENT,
    `Email` varchar(100) NOT NULL COMMENT '电子邮箱',
    PRIMARY KEY (`Id`)
);
insert into `Person` (Id, Email)
values (1, 'aaa@qq.com'),
       (2, 'aaa@qq.com'),
       (3, 'aaa@qq.com');
SELECT 
  Email 
FROM
  Person 
GROUP BY Email 
HAVING COUNT(Email) > 1 ;
SELECT
  Email
FROM
  (SELECT Email, COUNT(Email) AS num FROM Person GROUP BY Email) AS statistic
WHERE num > 1 ;

196. 删除重复的电子邮箱

DROP TABLE IF EXISTS `Person`;
CREATE TABLE `Person`
(
    `Id`    bigint(20)   NOT NULL AUTO_INCREMENT,
    `Email` varchar(100) NOT NULL COMMENT '电子邮箱',
    PRIMARY KEY (`Id`)
);
insert into `Person` (Id, Email)
values (1, 'aaa@qq.com'),
       (2, 'aaa@qq.com'),
       (3, 'aaa@qq.com'),
       (4, 'aaa@qq.com'),
       (5, 'aaa@qq.com');

196. 删除重复的电子邮箱

DELETE p1
    FROM Person p1, Person p2
WHERE
      p1.Email = p2.Email AND p1.Id > p2.Id;

 

DELETE 
  p1 
FROM
  Person p1 
  INNER JOIN Person p2 ON p1.Email = p2.Email AND p1.Id > p2.Id 

不准用组函数(MAX),取得最高薪水(给出两种解决方案):https://yuanyu.blog.csdn.net/article/details/106574112#t9

 

 

相关标签: oj刷题 面试