Check codes that are not in the database with a query

0

I know that I can check codes that are in one table and are not in another with the following query :

SELECT cod FROM tabela1 WHERE cod NOT IN (SELECT cod FROM tabela2)

But how can I check the records that are not in a table but are in a comma separated list?

Example: Let's say I have a names table with the following data:

cod | nome
----|-----------------
1   | César
3   | Maurício

And I want to make a query by passing the following 1, 2, 3, 4 faults expecting the return to be 2, 4 which are codes that are not in the database.

How can I do this?

    
asked by anonymous 26.01.2018 / 14:54

1 answer

0

If the list is fixed, you can do this with union s:

select * from (
  select 1 item union
  select 2 union 
  select 3 union
  select 4
)
where item not in (select cod from tabela1)

But as the list expands, the query will slow down.

Fiddle: link

    
26.01.2018 / 15:17