English 中文(简体)
表格中的重复条目
原标题:Get duplicate entries from a table
  • 时间:2010-07-30 10:20:32
  •  标签:
  • php
  • mysql

我怎么能从一张桌子上接过不止一次的囚室? 我允许我的网站用户每个电子邮件只开一个账户,但我的发言稿只有很少浏览,因此用户能够用同一个电子邮件地址登记不止一次。 我想得到不止一次记录的所有电子邮件。 如何使用php/mysql?

最佳回答

您不妨尝试如下内容:

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)
问题回答

select email from table group by email having count(email) > 1
should do the trick :)

利用这一询问:

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签