Search code examples
mysqlsqlgroup-concatcomposite-primary-key

Query multiple tables to find duplicate one to one relation (group by multiple column from diffeerent tables)


I have 2 tables

table1:

id columnA
1 one
2 two
3 one

table2 :

table1_id columnB
1 row1
2 row2
3 row1

Is it possible to have a single query to fetch me the id from table1 where duplicate value in columnA also has duplicate value in columnB:

Like in this example, id 1 and 3 from table1 has duplicate values in columnA as well as columnB


Solution

  • SELECT GROUP_CONCAT(table1.id)
    FROM table1 
    JOIN table2 ON table1.id = table2.table1_id
    GROUP BY table1.columnA, table2.columnB
    HAVING COUNT(*) > 1