Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:计算两个表中有多少行是相同的_Mysql_Sql_Count_Rows_Equals - Fatal编程技术网

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

MySQL:计算两个表中有多少行是相同的,mysql,sql,count,rows,equals,Mysql,Sql,Count,Rows,Equals,我有两张桌子,例如: +---------+---------+ | Table A | Table B | +---------+---------+ | 52 | 12 | | 64 | 6 | | 36 | 69 | | 48 | 52 | | 12 | | +---------+---------+ 我想知道在一个MySQL查询中,这些表中有多少行是相同的。 有什么帮助吗 (在我

我有两张桌子,例如:

+---------+---------+
| Table A | Table B |
+---------+---------+
|      52 |      12 |
|      64 |       6 |
|      36 |      69 |
|      48 |      52 |
|      12 |         |
+---------+---------+
我想知道在一个MySQL查询中,这些表中有多少行是相同的。 有什么帮助吗

(在我们的示例中2

您可以使用查找两个表之间的匹配行,然后使用
COUNT

查询

select count(*) from TableA a
where exists(
    select 1 from TableB b
    where a.col1 = b.col1
);
select count(distinct *) from TableA a
where exists(
    select 1 from TableB b
    where a.col1 = b.col1
);
如果你计算唯一值

查询

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

您可以使用JOIN语句对其进行计数:

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

使用
内部联接

假设表A有A列,表B有B列
因此,查询将是:

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

您可以使用
内部联接


从A.col1=B.col1上的表A内部联接表B中选择计数(*)

这些表的列名是什么?使用简单的内部联接向我们展示您的数据表结构?