[MySQL]LeetCode196 Delete Duplicate Emails
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]| +----+------------------+
題意:將一個表中重複的email行去掉只保留一行,且保留的Id最小。
解法一:
delete from Person where Id not in (select min_id from (select min(id) as min_id from Person group by Email) as tmp);
解法二:
delete p1 from Person p1 inner join Person p2 where p1.Email=p2.Email and p1.Id>p2.Id //內連線將重複的email找出來,再刪除
有人看了上面解法一的答案:會問select min_id from這句話好像沒啥用。得到的不還是內層select的結果麼?但是我去掉了出現如下問題。
You can't specify target table 'Person' for update in FROM clause 意思是,你不能在from的子句中,指定被用來更新的目標表。也就是在Mysql中在同一個表中不能select之後再update.所以將select出的內容作為tmp臨時表,然後從tmp中選出min_id再刪除。
相關推薦
[MySQL]LeetCode196 Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +
leetcode196-Delete Duplicate Emails(刪除重複並且id較大的資料)
問題描述: Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its sma
leetcode 196. Delete Duplicate Emails 刪除重複的電子郵箱 mySQL (delete where group order)
# Write your MySQL query statement below # First method DELETE p1 FROM Person p1, Person p2 WHERE p1
[LeetCode] Delete Duplicate Emails
rom log john tab bsp bob ping leetcode email Write a SQL query to delete all duplicate email entries in a table named Person, keeping onl
LeetCode Delete Duplicate Emails
Problem Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based
[LeetCode] Delete Duplicate Emails 刪除重複郵箱
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------
Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smalles
leetcode 196. Delete Duplicate Emails(SQL,刪除重複元組)39
貼原題: 解析: 本題是讓從Person表中刪除Email重複的元組。 那麼可以用自身連線寫法和子查詢寫法兩種。 自身連線即需要找出Email相等,Id最小的元組。 子查詢
leetcode 196. Delete Duplicate Emails delete
題意:刪除Email重複的行,保留Id最小的行,也就使每個Email只保留Id最小那個 思路:用刪除語句進行刪除 delete p1 from Person as p1, Person as
196. Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest I
【Leetcode】196. Delete Duplicate Emails (Easy)
1.題目Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.翻
深入mysql "ON DUPLICATE KEY UPDATE" 語法的分析
入行 能說 問題 values cor () 功能 copy 導致 轉:http://www.jb51.net/article/39255.htmmysql "ON DUPLICATE KEY UPDATE" 語法如果在INSERT語句末尾指定了ON DUPLICATE K
sql leetcode -Duplicate Emails
code etc 作用 region div 這樣的 技術 logs com 第一種解法: select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email an
mysql 中delete和trncate區別
重新 sql delet use 它的 刪除 掃描 進行 from mysql中刪除表記錄delete from和truncate table的用法區別: MySQL中有兩種刪除表中記錄的方法:(1)delete from語句,(2)truncate table語句。 d
mysql "ON DUPLICATE KEY UPDATE" 的使用
例子 lis 查看 字段名 style .sh eat 數據 foreach ON DUPLICATE KEY UPDATE 語法並不是SQL的標準語法,如果在句尾指定該語法,它會根據指定的主鍵或者唯一標示索引來更新數據庫的內容 具體的操作是想根據唯一標示查看數據庫是否存在
182. Duplicate Emails--solution
query name table div tab find AI ould ble Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id |
MySQL之delete 忘加where條件誤刪除恢復
MySQL之delete刪除恢復一、mysql環境介紹: mysql數據庫指定字符集位utf8,同時表的字符集也得為utf8,同時mysql要開啟row模式的bin-log日誌 /etc/my.cnf文件字符集參數設置: [root@git-server ~]# grep character-set /etc
MySQL之delete 忘加where條件誤刪除恢復方法二
delete忘加where條件誤刪除恢復和昨天介紹的MySQL之delete 忘加where條件誤刪除恢復的mysql的環境條件是一樣的:mysql數據庫指定字符集位utf8,同時表的字符集也得為utf8,同時mysql要開啟row模式的bin-log日誌 創建一張測試表測試: create table My
mysql INSERT ... ON DUPLICATE KEY UPDATE語句
not 股票 目的 一個 lin arc sta int prim 網上關於INSERT ... ON DUPLICATE KEY UPDATE大多數文章都是同一篇文章轉來轉去,首先這個語法的目的是為了解決重復性,當數據庫中存在某個記錄時,執行這條語句會更新它,而不存在這條
MySQL 中delete刪除sql語句用法
用法 where子句 語句 com del mysq 如何 子句 關鍵詞 mysql如何要刪除一個一行或者多行,用sql語句delete關鍵詞,固定用法 delete from 表名。 1,delete刪除一行 delete from stude