如何检查两个不同表中两列数据的存在?的MySQL [英] How to check the existence of two columns' data in two different tables ? MySQL
问题描述
我在两个不同的数据库中有两个不同的表..
I have two different tables in two different databases ..
我要做的是检查另一列中是否存在两列的数据..如果确实存在,请对其进行计数,最后我希望在另一张表中具有匹配项的记录数.
what I want to do is to check if the data of two columns exist in the other table .. if it does exist count it and at the final I want the number of records that have matching in the other table.
示例:
表_1
column_1值=狗"
column_1 value = "dog"
column_2值=苹果"
column_2 value = "apple"
table_2
column_1值=狗"
column_1 value = "dog"
column_2值=橙色"
column_2 value = "orange"
所以这里第一列的值在两个表中都存在,但是第二列是不同的,所以我不想对其进行计数..我想对两个值在同一记录中的位置进行计数.
so here the first column values exist in both table but the second column is different so I don't want to count it .. I want to count where both values exist in the same record .
ps:column_1和column_2都不是主键
ps: Both column_1 and column_2 aren't primary key
是否有使用MySQL的解决方案?因为我用Java解决了这个问题,但要花很长时间才能获得500万条记录.
is there a solution for it using MySQL ? because I used java to solve this but take a long time for 5 million records .
推荐答案
在两个表上都执行INNER JOIN
:
SELECT COUNT(*)
FROM table_1 t1
INNER JOIN table_2 t2
ON t1.column_1 = t2.column_1
AND t1.column_2 = t2.column_2
这篇关于如何检查两个不同表中两列数据的存在?的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!