How to get repeated records in mysql?

1

I have this table

+------+----------------+-----------------+
| id   | home_team_name | visit_team_name |
+------+----------------+-----------------+
| 1436 | Philadelphia   | Miami           |
| 1431 | Jacksonville   | Carolina        |
| 1426 | Detroit        | New England     |
| 1461 | Tennessee      | Chicago         |
| 1371 | Tennessee      | Chicago         |
| 1366 | Washington     | Cincinnati      |
| 1456 | Washington     | Cincinnati      |
| 1441 | Minnesota      | San Francisco   |
| 1361 | Minnesota      | San Francisco   |
+------+----------------+-----------------+

I would like to know how to make a query to show me the duplicates, which would be:

+------+----------------+-----------------+
| id   | home_team_name | visit_team_name |
+------+----------------+-----------------+
| 1461 | Tennessee      | Chicago         |
| 1371 | Tennessee      | Chicago         |
| 1366 | Washington     | Cincinnati      |
| 1456 | Washington     | Cincinnati      |
| 1441 | Minnesota      | San Francisco   |
| 1361 | Minnesota      | San Francisco   |
+------+----------------+-----------------+
    
asked by skycomputer2 25.08.2017 в 20:46
source

4 answers

2

Another way, it would be:

select  t1.id,
    t1.home_team_name,
    t1.visit_team_name 
    from tu_tabla t1
    inner join ( select     home_team_name,
                visit_team_name 
            from tu_tabla
            group by home_team_name,
                visit_team_name 
            having count(1) > 1
    ) t2
    on t1.home_team_name = t2.home_team_name
    and t1.visit_team_name = t2.visit_team_name

The subquery only retrieves those cases where home_team_name and visit_team_name are repeated by means of group by and having count(1) > 1

    
answered by 25.08.2017 в 21:19
1

You need to look for those records where there is another with the same data, but different id . For this you can create a sub-query with EXISTS

Example:

SELECT M.*
FROM matchs AS M
WHERE EXISTS (
    SELECT 1
    FROM matchs
    WHERE home_team_name = M.home_team_name
        AND  visit_team_name = M.visit_team_name
        AND id != M.id
    LIMIT 1)

Demo

    
answered by 25.08.2017 в 21:13
0

First you must obtain the list of repeated cases, to later list each one of them

For example:

select id, home_team_name, visit_team_name
from mytable 
where home_team_name in (
    select home_team_name
    from mytable 
    group by  home_team_name
    having count(*)>1)
    
answered by 25.08.2017 в 21:11
0

To see which ones have duplicates, the query would be:

SELECT * FROM mitabla 
INNER JOIN 
(SELECT home_team_name FROM mitabla GROUP BY home_team_name, visit_team_name HAVING COUNT(*)>1) sel
ON mitabla.home_team_name = sel.home_team_name

You need to see the records within those that are duplicated for the fields that interest you, which are collected in the parentheses selection.

    
answered by 25.08.2017 в 21:19