Skip to content
Advertisement

Update parent table column only if all the values of child table rows are a specific value

I have 2 tables, orders and order_items. Each order can have multiple order_items.

An order has an id and orderStatus:

enter image description here

An order_item as id, orderId and orderItemStatus: enter image description here

I want to update the status of order’s from OPEN to FULFILLED if all an order’s order_items have status of ISSUED. If at least one order_item is not ISSUED, the order status should remain as OPEN. How can I do this?

Advertisement

Answer

You can use a NOT EXISTS subquery as condition.

This will only update orders which have status ‘OPEN’ and have no items with status ‘ISSUED’.

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