2010-07-30 75 views
2

如何獲取從表中插入多次的行?我允許我的網站用戶每個電子郵件只有一個帳戶,但我的腳本有一個小錯誤,因此用戶可以使用相同的電子郵件地址註冊多次。我想獲取所有已經記錄多次的電子郵件。這怎麼可能使用PHP/MySQL?從表中獲取重複條目

回答

4

你可能想嘗試一些類似如下:

SELECT  email_address, 
      COUNT(*) number_of_dupes, 
      GROUP_CONCAT(user_id) users 
FROM  users 
GROUP BY email_address 
HAVING  COUNT(*) > 1; 

測試用例:

CREATE TABLE users (user_id int, email_address varchar(30)); 

INSERT INTO users VALUES (1, '[email protected]'); 
INSERT INTO users VALUES (2, '[email protected]'); 
INSERT INTO users VALUES (3, '[email protected]'); 
INSERT INTO users VALUES (4, '[email protected]'); 
INSERT INTO users VALUES (5, '[email protected]'); 
INSERT INTO users VALUES (6, '[email protected]'); 

結果:

+-------------------+-----------------+-------+ 
| email_address  | number_of_dupes | users | 
+-------------------+-----------------+-------+ 
| [email protected] |    2 | 1,3 | 
| [email protected] |    3 | 2,4,6 | 
+-------------------+-----------------+-------+ 
2 rows in set (0.00 sec) 
3

select email from table group by email having count(email) > 1
應該做的伎倆:)

2

使用此查詢:

SELECT email FROM tableName GROUP BY email HAVING count(email) > 1