196. Delete Duplicate Emails - 删除重复的电子邮箱 <easy>

本文介绍了一个SQL查询案例,用于从Person表中删除所有重复的电子邮箱记录,仅保留每个重复邮箱中Id最小的一条记录。通过使用子查询和delete语句实现这一目标。

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

提示:

执行 SQL 之后,输出是整个 Person 表。
使用 delete 语句。

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
Id is the primary key column for this table.
For example, after running your query, the above Person table should have the following rows:
Note:Your output is the whole Person table after executing your sql. Use delete statement.

思路:删除掉出现多次的邮箱记录,id not in (id ~ count(Email)>1) , 并且保证只有一个邮箱的不被删除

delete from Person 
where id not in (
    select id from (select min(Id) id from Person group by Email having count(Email) > 1) a
) and Email not in (
    select Email from (select Email from Person group by Email having count(Email) = 1) b
)

更简单的想法是只保留第一次出现的邮箱:

DELETE from Person 
Where Id not in (
    Select Id 
    From(
    Select MIN(Id) as id
    From Person 
    Group by Email
   ) t
)

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.jutadata</groupId> <artifactId>jtf_esp</artifactId> <version>2.0.2-esp-mz</version> </parent> <artifactId>jtf-crm</artifactId> <packaging>pom</packaging> <description>CRM系统功能</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.jutadata</groupId> <artifactId>jtf-common</artifactId> <version>${jtf.esp.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project> Could not find artifact org.mybatis.spring.boot:mybatis-spring-boot-starter:pom:unknown in aliyun-repos (http://maven.aliyun.com/nexus/content/groups/public/) Could not find artifact org.mybatis.spring.boot:mybatis-spring-boot-starter:pom:unknown in aliyun-repos (http://maven.aliyun.com/nexus/content/groups/public/) 请帮我解决这个错误
最新发布
07-15
### 解决 Maven 构建错误:依赖版本缺失与重复声明 在使用 Maven 构建项目时,如果出现 `dependencies.dependency.version` 缺失以及依赖项重复声明的问题,可能导致构建失败。以下是对该问题的分析与解决方案。 #### 1. **明确指定依赖版本** Maven 要求每个 `<dependency>` 都应显式声明其版本号,除非该依赖已被父 POM 的 `<dependencyManagement>` 所管理。若未正确配置,会出现如 `org.mybatis.spring.boot:mybatis-spring-boot-starter` 缺少版本信息的错误[^1]。 **解决方法:** ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <!-- 显式指定版本 --> </dependency> ``` 确保所有直接使用的依赖都包含 `<version>` 标签,以避免因版本缺失导致构建失败。 #### 2. **处理依赖重复声明** 当同一个依赖(例如 `mysql:mysql-connector-java`)被多次声明,且版本不一致或未指定版本时,Maven 会提示“duplicate declaration”警告。这类问题常见于多模块项目中,不同子模块引入了相同依赖但版本不同。 **解决方法:** - **统一版本号:** 在父 POM 的 `<dependencyManagement>` 中定义统一版本: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> </dependencies> </dependencyManagement> ``` - **子模块中无需再写版本号:** ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 这样可以有效防止重复声明和版本冲突。 #### 3. **使用 `mvn dependency:tree` 分析依赖树** 执行以下命令可查看项目的完整依赖结构,并识别哪些依赖被重复引入或存在冲突: ```bash mvn dependency:tree ``` 通过分析输出结果,可以定位到具体是哪个模块引入了重复的依赖,从而进行针对性修复。 #### 4. **排除冲突依赖** 如果某个依赖间接引入了不需要的版本,可以使用 `<exclusion>` 排除它。例如: ```xml <dependency> <groupId>some-group</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </exclusion> </exclusions> </dependency> ``` 这种方式可以控制依赖链中的具体版本,避免冲突。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值