简体   繁体   中英

Getting Duplicates from two columns in different tables

I have been looking at this all day. I am trying to return a list of duplicates from two columns in two separate tables.

In MYSQL the only way to get a full outer join seems to be with a UNION, which I have tried:

select mobile from firstTable
group by mobile
having count(mobile) > 1
union all
select mobile from secondTable
group by mobile
having count(mobile) > 1;

However, this gets duplicates in the same tables, and doesn't account for across table duplicates. I have also tried joins, however I don't know how to then check for duplicates across the separate columns?

My intent is to get duplicates as if both columns where 1 and I am counting those with multiple entries. Any help is appreciated, cheers.

I recommend taking a union first, then aggregating the combined result:

SELECT mobile
FROM
(
    SELECT mobile FROM firstTable
    UNION ALL
    SELECT mobile FROM secondTable
) t
GROUP BY mobile
HAVING COUNT(mobile) > 1;

Note: If within each of the two tables, a given mobile value could appear more than once, then use SELECT DISTINCT mobile FROM some_table to first remove the duplicates.

I'm a beginner, this answer may help other beginners too in finding duplicates I would try creating a view contains both columns to avoid any confusion

CREATE VIEW newTable as (SELECT T1.columnA, T2.columnB from table1 join table2 on T1.id=T2.id)

And then do the selection from this new table

SELECT columnA, columnB, COUNT(*) FROM newTable GROUP BY A, B HAVING COUNT(*) > 1

The output should be the row that is duplicate in column A & column B together not in each individually like the following example:

T1 ( water bottles I have in the store) and T2 ( water bottles that have been bought) and say that I want to find how many water bottles do I have with same brand and how many times this brand was bought by this visa card particularly... This is what I understand you trying to do The output should this table 图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM