简体   繁体   English

MySQL:计算两个表的多少行相同

[英]MySQL: count how many rows of two tables are the same

I have two tables, for example: 我有两个表,例如:

+---------+---------+
| Table A | Table B |
+---------+---------+
|      52 |      12 |
|      64 |       6 |
|      36 |      69 |
|      48 |      52 |
|      12 |         |
+---------+---------+

And I want to find how many rows of those tables are the same in one MySQL query. 我想找到一个MySQL查询中这些表的多少行相同。 Any help? 有什么帮助吗?

(In our example 2 ) (在我们的示例2中

You can use EXISTS to find the matching rows between the two tables and then use COUNT . 您可以使用EXISTS查找两个表之间的匹配行,然后使用COUNT

Query 询问

select count(*) from TableA a
where exists(
    select 1 from TableB b
    where a.col1 = b.col1
);

And if you count the unique values, 如果您计算唯一值,

Query 询问

select count(distinct *) from TableA a
where exists(
    select 1 from TableB b
    where a.col1 = b.col1
);

You can use JOIN statement to counting it: 您可以使用JOIN语句对其进行计数:

SELECT COUNT(*) FROM tb1
JOIN tb2 ON tb1.ColumnA = tb2.ColumnA

Use INNER JOIN 使用内INNER JOIN
Let's assume TableA have column A and TableB have column B 假设TableA的A列和TableB的B列
So the query will be : 因此查询将是:

SELECT count(*) FROM TableA INNER JOIN TableB ON (A = B);

您可以使用INNER JOIN

SELECT COUNT(*) FROM TableA A INNER JOIN TableB B on A.col1=B.col1

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

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