SQL Abfrage für Zeilen die nicht in anderer Tabelle vorhanden ist

Aus MyWiki
Zur Navigation springen Zur Suche springen

Gibt Zeilen aus Tablle1 zurück, die in der zweiten Tabelle nicht vorhanden sind

 SELECT * FROM table1 t1
 WHERE NOT EXISTS
 (
    SELECT 1
    FROM table2 t2
    WHERE t2.id = t1.id
 }



Diese Variante arbeitet noch mit einem JOIN und gibt eine Collation mit, da die Tabellen unterschiedlichen Collationen haben.

 SELECT * FROM table1 t1
 LEFT JOIN table3 t3 ON t1.id = t3.id COLLATE latin1_german2_ci
 WHERE NOT EXISTS
 (
    SELECT 1
    FROM table2 t2
    WHERE t2.id=t1.id COLLATE latin1_german2_ci
 )
 AND t3.active = 1 COLLATE latin1_german2_ci