Mysql
 SQL >> Database >  >> RDS >> Mysql

Come confrontare due tabelle e trovare le differenze

Puoi provare con una outer join. Ad esempio, per trovare le righe presenti nella tabella first ma assenti nella tabella second, puoi usare questa query (non testata):

SELECT first.a, first.b, first.c FROM first LEFT JOIN second USING(a,b,c) WHERE second.a IS NULL 

La join produce una tabella che contiene tutte le righe presenti in first, in questo modo:

first.a first.b first.c second.a second.b second.c a1 b1 c1 NULL NULL NULL a2 b2 c2 a2 b2 c2 

A questo punto basta filtrare le righe con second.a IS NULL per individuare quelle assenti in second.

Le prestazioni potrebbero non essere ottimali, perché sei costretto a fare la join su tutte le colonne.