Puoi utilizzare un left outer join
per realizzare questo:
select
t1.tid
from
table1 t1
left outer join table2 t2 on
t1.tid = t2.tid
where
t2.tid is null
Ciò che fa è prendere la tua prima tabella (table1 ), si unisce alla tua seconda tabella (table2 ), e inserisce null per la table2 colonne in qualsiasi riga in table1 che non corrisponde a una riga in table2 . Quindi, lo filtra selezionando solo la table1 righe in cui non è stata trovata alcuna corrispondenza.
In alternativa, puoi anche utilizzare not exists
:
select
t1.tid
from
table1 t1
where
not exists (select 1 from table2 t2 where t2.tid = t1.tid)
Questo esegue un left semi join , e essenzialmente farà la stessa cosa che il left outer join fa. A seconda dei tuoi indici, uno potrebbe essere più veloce dell'altro, ma entrambe sono opzioni praticabili. MySQL ha una buona documentazione sull'ottimizzazione dei join , quindi dovresti verificarlo.