SELECT t1.tasknumber
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.id AND t2.document = 'SB'
WHERE t2.id IS NULL;
There are basically four techniques:
Question
I have two tables with a common id, table1 has a task numbers column and table2 has documents column each task can have multiple documents. I'm trying to find all task numbers that don't have a specific document
Fake data:
SELECT * FROM table1
id tasknumber
1 3210-012
2 3210-022
3 3210-032
SELECT * FROM table2
id document
1 revision1
1 SB
1 Ref
2 revision1
2 Ref
3 revision1
3 SB
But how would I find tasknumbers which don't have a document named SB?
La solution
SELECT t1.tasknumber
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.id AND t2.document = 'SB'
WHERE t2.id IS NULL;
There are basically four techniques:
Autres conseils
select t1.tasknumber from table1 t1
where not exists
(select 1 from table2 t2 where t1.id = t2.id and t2.document = 'SB')
select
tasknumber
from
table1
where
not exists (select 1
from table2
where table1.id = table2.id
and table2.document = 'SB');
or
select
tasknumber
from
table1
where
id not in (select id
from table2
where document = 'SB');
Use this:
SELECT TASKNUMBER
FROM TABLE1
WHERE ID NOT IN (SELECT DISTINCT ID FROM TABLE 2 where document = 'SB')