如何从表中获取多次插入的行?我允许我的网站用户每封电子邮件只有一个帐户,但我的脚本有一个小错误,因此用户可以使用相同的电子邮件地址注册多次。我希望获得多次记录的所有电子邮件。怎么可能使用php / mysql?
答案 0 :(得分: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, 'info1@example.com');
INSERT INTO users VALUES (2, 'info2@example.com');
INSERT INTO users VALUES (3, 'info1@example.com');
INSERT INTO users VALUES (4, 'info2@example.com');
INSERT INTO users VALUES (5, 'info3@example.com');
INSERT INTO users VALUES (6, 'info2@example.com');
结果:
+-------------------+-----------------+-------+
| email_address | number_of_dupes | users |
+-------------------+-----------------+-------+
| info1@example.com | 2 | 1,3 |
| info2@example.com | 3 | 2,4,6 |
+-------------------+-----------------+-------+
2 rows in set (0.00 sec)
答案 1 :(得分:3)
select email from table group by email having count(email) > 1
应该做的伎俩:)
答案 2 :(得分:2)
使用此查询:
SELECT email FROM tableName GROUP BY email HAVING count(email) > 1