Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for an alternative SQL command to find the same values in two tables

Tags:

sql

mysql

I am using this command to find the same values in two tables when the tables have 100-200 records. But When the tables have 100000-20000 records, the sql manager, browsers, shortly the computer is freesing.

Is there any alternative command for this?

SELECT
distinct
names
FROM
table1
WHERE
names in (SELECT names FROM table2)
like image 434
htaccess Avatar asked Dec 09 '25 10:12

htaccess


2 Answers

Try with join

SELECT distinct t1.names
FROM table1 t1
join table2 t2 on t2.names = t1.names
like image 116
Robert Avatar answered Dec 11 '25 23:12

Robert


Use EXISTS:

SELECT distinct t1.names
FROM Table1 t1
WHERE EXISTS(
    SELECT 1 FROM tabl2 t2 WHERE t2.names=t1.names
)
like image 44
Tim Schmelter Avatar answered Dec 12 '25 01:12

Tim Schmelter