i have a main table categories like this
categories_id | categories_status | parent_id
1 1 0
2 1 0
3 1 1
4 1 2
5 1 1
6 1 2
and a reference table products_to_categories
categories_id | products_id
3 778
3 998
5 666
5 744
I select all the categories with no child-category:
SELECT * FROM categories
WHERE categories_id not in ( SELECT parent_id FROM categories )
# gets entries with id 3, 4, 5, 6
and no products in reference table:
AND categories_id NOT IN ( SELECT categories_id FROM products_to_categories )
# gets entries with id 4, 6
Now i would like to update the categories_status for this result but it don’t work just changing SELECT to UPDATE:
UPDATE categories
SET categories_status = 0
WHERE categories_id not in ( SELECT parent_id FROM categories )
AND categories_id NOT IN ( SELECT categories_id FROM products_to_categories )
There a few similar questions, but i can’t figure out how to change my example…
Thanks & best regards,
Alex
Instead of sub-queries, use a left outer join