简体   繁体   English

从不同表中的两列获取重复项

[英]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:在 MYSQL 中,获得完整外连接的唯一方法似乎是使用 UNION,我已经尝试过:

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.我的目的是获得重复项,就好像 1 和我正在计算具有多个条目的列一样。 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.注意:如果在两个表中的每一个中,给定的mobile值可能出现多次,然后使用SELECT DISTINCT mobile FROM some_table首先删除重复项。

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:输出应该是在 A 列和 B 列中重复的行,而不是像下面的例子那样单独出现:

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 T1(我在店里有的水瓶)和T2(已购买的水瓶)并说我想知道我有多少个同品牌的水瓶以及这个品牌被这张签证卡购买了多少次...这就是我理解你想要做的输出应该是这张表图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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