[英]Find rows that have the same value on a column in MySQL
In a [member] table, some rows have the same value for the email
column.在 [member] 表中,某些行的
email
列具有相同的值。
login_id | email
---------|---------------------
john | john123@hotmail.com
peter | peter456@gmail.com
johnny | john123@hotmail.com
...
Some people used a different login_id but the same email address, no unique constraint was set on this column.有些人使用了不同的 login_id 但使用了相同的电子邮件地址,此列没有设置唯一约束。 Now I need to find these rows and see if they should be removed.
现在我需要找到这些行,看看是否应该删除它们。
What SQL statement should I use to find these rows?我应该使用什么 SQL 语句来查找这些行? (MySQL 5)
(MySQL 5)
This query will give you a list of email addresses and how many times they're used, with the most used addresses first.此查询将为您提供电子邮件地址列表及其使用次数,最常使用的地址在前。
SELECT email,
count(*) AS c
FROM TABLE
GROUP BY email
HAVING c > 1
ORDER BY c DESC
If you want the full rows:如果你想要完整的行:
select * from table where email in (
select email from table
group by email having count(*) > 1
)
select email from mytable group by email having count(*) >1
Here is query to find email
's which are used for more then one login_id
:这是查找用于多个
login_id
email
的查询:
SELECT email
FROM table
GROUP BY email
HAVING count(*) > 1
You'll need second (of nested) query to get list of login_id
by email
.您需要第二个(嵌套的)查询才能通过
email
获取login_id
列表。
First part of accepted answer does not work for MSSQL.已接受答案的第一部分不适用于 MSSQL。
This worked for me:这对我有用:
select email, COUNT(*) as C from table
group by email having COUNT(*) >1 order by C desc
use this if your email column contains empty values如果您的电子邮件列包含空值,请使用此选项
select * from table where email in (
select email from table group by email having count(*) > 1 and email != ''
)
Thanks guys :-) I used the below because I only cared about those two columns and not so much about the rest.谢谢大家 :-) 我使用了下面的内容,因为我只关心这两列,而不太关心其余的列。 Worked great
工作得很好
select email, login_id from table
group by email, login_id
having COUNT(email) > 1
I know this is a very old question but this is more for someone else who might have the same problem and I think this is more accurate to what was wanted.我知道这是一个非常古老的问题,但对于可能有同样问题的其他人来说更是如此,我认为这更符合我们想要的。
SELECT * FROM member WHERE email = (Select email From member Where login_id = john123@hotmail.com)
This will return all records that have john123@hotmail.com as a login_id value.这将返回所有具有 john123@hotmail.com 作为 login_id 值的记录。
Get the entire record as you want using the condition with inner select query.使用带有内部选择查询的条件,根据需要获取整个记录。
SELECT *
FROM member
WHERE email IN (SELECT email
FROM member
WHERE login_id = abcd.user@hotmail.com)
Very late to this thread, but I had a similar situation and the following worked on MySQL.这个线程很晚了,但我遇到了类似的情况,以下在 MySQL 上工作。 The following query will also return all the rows that match the condition of duplicate emails
以下查询还将返回与重复电子邮件条件匹配的所有行
SELECT * FROM TABLE WHERE EMAIL IN
(SELECT * FROM
(SELECT EMAIL FROM TABLE GROUP BY EMAIL HAVING COUNT(EMAIL) > 1)
AS X);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.