![](/img/trans.png)
[英]Update a column value to the COUNT of rows for specific values in same table
[英]Update parent table column only if all the values of child table rows are a specific value
您可以使用 NOT EXISTS 子查詢作為條件。
update order o
set o.orderStatus = 'FULFILLED'
where o.orderStatus = 'OPEN'
and not exists (
select *
from order_item i
where i.orderId = o.id
and i.orderItemStatus <> 'ISSUED'
)
這只會更新狀態為“OPEN”且沒有狀態為“ISSUED”的商品的訂單。
如果這是唯一狀態,您可以檢查狀態為 ISSUED 的 orders_item
update orders o
inner join (
select orderID, count( dictinct orderItemStatus)
from order_item
group by orderID
having count( dictinct orderItemStatus) = 1
) t on t.orderID = o.id
inner join order_item i on i.orderID = o.id
AND orderItemStatus = 'ISSUED'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.