Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

434
Visualizações
Postgres, get all items from one table which ids are not in another tables JSON array column?

I have two tables:

cars table which contains:

id | name
1  | Audi
2  | Mercedes
3  | BMW

electric_cars

id | cars_id | non_valid_cars (json)
1  | 1       | [1,3]
2  | 3       | [1]
3  | 2       | [2,3]

How to select all records from the cars table which are not in the non_valid_cars array of ids in the electric_cars column with id cars_id?

Also, I am using Laravel Framework, but I will translate a query into the framework.

Thank you so much for your help.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use a NOT EXISTS condition:

select c.*
from cars c
where not exists (select * 
                  from electric_cars ec
                  where ec.non_valid_cars::jsonb @> to_jsonb(c.id)
                    and ec.cars_id = c.id);

Note that the use of jsonb is recommended over json so you might want to change that to avoid the casting.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda