Find values ​​that do not allow connecting to sql

Let's say I have a request, for example:

SELECT * FROM Table_1 JOIN Table_2 ON Table_1.Col_1 = Table_2.Col_1 

So, I have 100 records and 98 of them are equal, so the query will print 98 out of 100. How can I get SQL to print 2 that could not complete the connection?

+4
source share
4 answers

Use LEFT JOIN :

 SELECT * FROM Table_1 LEFT JOIN Table_2 ON (Table_1.Col_1 = Table_2.Col_1) 

Fields Table_2 will be NULL, where there was no match for the ON clause. Then you can add WHERE TABLE_2.Col_1 IS NULL to save only records in Table_1 that did not have a match in Table_2 .

+6
source

An alternative to LEFT JOIN is to use EXISTS.

 SELECT * FROM Table_1 WHERE NOT EXISTS (SELECT * FROM Table_2 WHERE Col_1 = Table_1.Col_1) 
+7
source

try the following:

 SELECT * FROM `Table_1` LEFT JOIN `Table_2` ON (`Table_1`.`Col_1` = `Table_2`.`Col_1`) 
0
source
 SELECT * FROM Table1 AS A LEFT OUTER JOIN Table2 AS B ON (A.Col = B.Col) WHERE B.Col IS NULL 
0
source

All Articles