Reputation: 1382
I have 2 tables product_product
and product_location_stock
.
table product_product
doesn't have any relation to product_location_stock
product_location_stock
have product_id
column and it is relation to product_product
and my goal is to get ids of all products that do not have product_location_stock
there are some products that are assigned to product_location_stock and there is some that are not, so basically I need to get ids of those that not.
so first I thought that I need to get all product_ids like
SELECT id FROM product_product
and after
SELECT product_id from product_loction_stock
and maybe then compare id's and get ids that don't match?
and after that I'm kinda stuck, so is it even possible to accomplish what I want?
Upvotes: 0
Views: 49
Reputation: 463
Below code may help you.
SELECT P.product_id FROM product_product P
left join product_location_stock PL ON PL.product_id =P.product_id
WHERE PL.product_id IS NULL
Upvotes: 2
Reputation: 247535
NOT EXISTS
is the direct translation of your requirement into SQL:
SELECT product_product.id
FROM product_product
WHERE NOT EXISTS
(SELECT 1
FROM product_location_stock
WHERE product_location_stock.product_id = product_product.id);
Upvotes: 3