leetcode196-Delete Duplicate Emails(중복되고 id가 큰 데이터 삭제)

2267 단어
문제 설명:
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 | Email | +----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
| 3 | [email protected] | +----+------------------+

Id is the primary key column for this table. For example, after running your query, the above Person table should have the following rows:
+----+------------------+
| Id | Email | +----+------------------+
| 1  | [email protected] |
| 2 | [email protected] | +----+------------------+

문제 해결:
1. MySQL DELETE 문법의 다중 테이블 문법: 임시 테이블을 만들고 삭제 조건을 찾아 삭제 업데이트를 합니다.
delete p1 from Person p1 inner join Person p2 where p1.email=p2.email and p1.id>p2.id;

또는:
delete from p1 using Person p1 inner join Person p2 where p1.email=p2.email and p1.id>p2.id;

2、
delete from Person where Id not in (select * from (select min(Id) from Person p group by email) mindata);

좋은 웹페이지 즐겨찾기