如何将此SELECT语句更改为UPDATE语句?

时间:2013-06-13 15:16:26

标签: mysql sql

我设法编写了一个查询,它可以撤回单独表中没有项目的订单。我现在想根据返回的记录集更新xxx_orders。可以在一个查询中完成吗?

SELECT Orders.OrderNumber,
       Orders.CustomerNumber,
       AffiliateCode
FROM   xxx_orders AS Orders
       LEFT OUTER JOIN xxx_items AS Items
         ON Orders.OrderNumber = Items.OrderNumber
WHERE  DATEDIFF(NOW(), OrderDate) >= 14
       AND Orders.Product = 'HOUSEHOLD'
       AND STATUS = 'BOOKING'
       AND Completed = 1
       AND Items.Id IS NULL
ORDER  BY OrderDate DESC 

1 个答案:

答案 0 :(得分:1)

通过MySQL中的JOIN进行更新的基本语法如下所示:

UPDATE xxx_orders AS Orders
LEFT OUTER JOIN xxx_items AS Items
         ON Orders.OrderNumber = Items.OrderNumber
SET  Orders.CancelledReason = 'Expired'
WHERE  DATEDIFF(NOW(), OrderDate) >= 14
       AND Orders.Product = 'HOUSEHOLD'
       AND STATUS = 'BOOKING'
       AND Completed = 1
       AND Items.Id IS NULL

编辑:SET出现在WHERE之前