If I were to have these three tables (just an example in order to learn UNION, these are not real tables):
Tables with their columns:
Customer:
id | name | order_status
Order_Web:
id | customer_id | order_filled
Order:
id | customer_id | order_filled
And I wanted to update order_status in the Customer table when there is a order filled in either the Order_Web table or the Order table for that customer using Union:
UPDATE c
SET c.order_status = 1
FROM Customer AS c
INNER JOIN Order_Web As ow
ON c.id = ow.customer_id
WHERE ow.order_filled = 1
UPDATE c
SET c.order_status = 1
FROM Customer AS c
INNER JOIN Order As o
ON c.id = o.customer_id
WHERE o.order_filled = 1
How can I combine these two updates using a Union on order_web and order?
Using Microsoft SQL Server Management Studio
UPDATE c
SET c.order_status = 1
FROM (
SELECT customer_id
FROM order_web
WHERE order_filled = 1
UNION
SELECT customer_id
FROM order
WHERE order_filled = 1
) o
JOIN customer c
ON c.id = o.customer_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With