Znajdź wiersze, które mają tę samą wartość w kolumnie w MySQL

W tabeli [member] niektóre wiersze mają taką samą wartość dla kolumny email.

login_id | email
---------|---------------------
john     | [email protected]
peter    | [email protected]
johnny   | [email protected]
...

Niektórzy ludzie używali innego login_id, ale tego samego adresu e-mail, nie ustawiono unikalnego ograniczenia w tej kolumnie. Teraz muszę znaleźć te rzędy i zobaczyć, czy powinny być usunięte.

Jakiego polecenia SQL powinienem użyć, aby znaleźć te wiersze? (MySQL 5)

Author: Adi Inbar, 2009-11-23

9 answers

To zapytanie daje listę adresów e-mail i ile razy są one używane, z najczęściej używanych adresów najpierw.

SELECT email,
       count(*) AS c
FROM TABLE
GROUP BY email
HAVING c > 1
ORDER BY c DESC

Jeśli chcesz mieć pełne wiersze:

select * from table where email in (
    select email from table
    group by email having count(*) > 1
)
 362
Author: Scott Saunders,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-20 05:29:47
select email from mytable group by email having count(*) >1
 59
Author: HLGEM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-23 22:36:11

Oto zapytanie do znalezienia email ' s, które są używane dla więcej niż jednego login_id:

SELECT email
FROM table
GROUP BY email
HAVING count(*) > 1

Będziesz potrzebował drugiego (zagnieżdżonego) zapytania, aby uzyskać listę login_id przez email.

 14
Author: Ivan Nevostruev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-23 22:36:35

Pierwsza część zaakceptowanej odpowiedzi nie działa dla MSSQL.
To mi się udało:

select email, COUNT(*) as C from table 
group by email having COUNT(*) >1 order by C desc
 10
Author: Sergey Makhonin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-08 09:27:37

Użyj tego, jeśli kolumna e-mail zawiera puste wartości

 select * from table where email in (
    select email from table group by email having count(*) > 1 and email != ''
    )
 5
Author: ramesh kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-17 17:26:12

Wiem, że to bardzo stare pytanie, ale to jest bardziej dla kogoś innego, kto może mieć ten sam problem i myślę, że jest to bardziej dokładne do tego, co było chciał.

SELECT * FROM member WHERE email = (Select email From member Where login_id = [email protected]) 

To zwróci wszystkie rekordy, które mają [email protected] jako wartość login_id.

 3
Author: Marc L,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-18 14:19:45

Dzięki chłopaki: -) użyłem poniższego, bo zależało mi tylko na tych dwóch kolumnach, a nie tyle na reszcie. Worked great

  select email, login_id from table
    group by email, login_id
    having COUNT(email) > 1
 2
Author: Libertine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-11 09:21:25

Pobierz cały rekord, jak chcesz, używając warunku z wewnętrznym zapytaniem select.

SELECT *
FROM   member
WHERE  email IN (SELECT email
                 FROM   member
                 WHERE  login_id = [email protected]) 
 1
Author: Suba Karthikeyan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-08 10:31:37

To działa najlepiej

Zrzut ekranu Tutaj wpisz opis obrazka

SELECT RollId, count(*) AS c 
    FROM `tblstudents` 
    GROUP BY RollId 
    HAVING c > 1 
    ORDER BY c DESC
 0
Author: Adeleye Ayodeji,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-12-16 17:43:06