Answer the question
In order to leave comments, you need to log in
MYSQL: Is it possible to write an update query if the condition has join, count, group by and having?
Request to select product IDs that do not have any images:
select product.id as id, count(product_image.id) as COUNT from product left join product_image on product.id = product_image.product_id group by product.id having count = 0;
update product set disabled = 1 where id in (select product.id as id, count(product_image.id) as COUNT from product left join product_image on product.id = product_image.product_id group by product.id having count = 0);
UPDATE
product PRODUCT
LEFT JOIN product_image IMAGE
ON IMAGE.product_id = PRODUCT.id
SET PRODUCT.disabled = 1
WHERE
IMAGE.id is NULL;
Answer the question
In order to leave comments, you need to log in
Can it be like this
UPDATE product SET disabled = 1 WHERE id IN (
SELECT product.id FROM product LEFT JOIN product_image ON product.id = product_image.product_id GROUP BY product.id HAVING COUNT(product_image.id) = 0);
UPDATE product SET disabled = 1 WHERE NOT EXISTS (SELECT 1 FROM product_image WHERE product_id = product.id)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question