Skip to content
Advertisement

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:

JavaScript

electric_cars

JavaScript

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.

Advertisement

Answer

You can use a NOT EXISTS condition:

JavaScript

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement