2013-03-27 70 views
1

我有格式的表:刪除冗餘信息,難以SQL查詢

`users` 
- id 
- views 
- email 

這裏有一些項目,我有:

`id`  `email`     `likes` 
9570800 [email protected]  888 
5355388 [email protected]  3042 
8610061 [email protected]   450 
6189810 [email protected] 0 
6625684 [email protected]  0 
16841675 [email protected]  40 
9716153 [email protected]  0 
9716353        0 
97161453        0 

我想DELETE有一個重複的電子郵件中的所有條目並保留最高的條目likes。例如,在上面,爲[email protected]的副本將意味着66256849716153既能被刪除,16841675將保持(因爲它有最喜歡出重複的。

此外,如果email是空白(''NULL),忽略它/不要刪除它。

我將如何做到這一點的查詢?

+0

好的網頁播放和測試與MySQL sql語句:http://www.sqlfiddle.com/ #!2/2440d/1 – user1050755 2013-03-27 03:44:18

回答

2
DELETE lesser FROM MyTable AS greater JOIN MyTable AS lesser USING (email) 
WHERE greater.email <> '' AND greater.likes > lesser.likes; 
+0

您可能還想添加'和greater.email不爲空' – Jeshurun 2013-03-27 03:45:23

+0

@Jeshurun,好主意,但是這是由連接處理的。如果'greater.email = lesser.email',電子郵件永遠不會爲空。 – 2013-03-27 13:00:11

0
DELETE FROM Users WHERE NOT userid in (
    SELECT userid FROM Users u 
     INNER JOIN (SELECT userid,max(likes) ml FROM Users GROUP BY userid) u2 
      ON u.userid = u2.userid AND u.likes = u2.ml 
    ) 
0

杉杉t,小心點。回顧潛力刪除列表:

SELECT * FROM users 
INNER JOIN (
    SELECT email, max(likes) max_likes FROM users GROUP BY email HAVING count(*) > 1 
) keep ON users.email = keep.email 
WHERE users.likes <> keep.max_likes 

然後,如果看起來還好你,代替SELECT *DELETE users

+0

max(喜歡)的引用在哪裏? – David542 2013-03-27 03:46:57

+1

子查詢SELECT電子郵件,max(喜歡)max_likes FROM用戶GROUP BY電子郵件HAVING count(*)> 1',將行列表返回到** keep **。 – 2013-03-27 03:47:48

+0

'max(likes)max_likes'命名列並聚合該電子郵件地址的最大喜歡數。 – 2013-03-27 03:48:42