Reputation: 1764
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.
example :
table_1
column_1 value = "dog"
column_2 value = "apple"
table_2
column_1 value = "dog"
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: Both column_1 and column_2 aren't primary key
is there a solution for it using MySQL ? because I used java to solve this but take a long time for 5 million records .
Upvotes: 0
Views: 2797
Reputation: 62861
If I'm understanding you correctly, one option is to use exists
:
select *
from table1 t1
where exists (
select 1
from table2 t2
where t1.column1 = t2.column1 and
t1.column2 = t2.column2
)
This will return a list of rows from the first table that have a corresponding matching row in the second.
Upvotes: 1
Reputation: 31879
Do an INNER JOIN
on both tables:
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
Upvotes: 1