通过从另一个表MySQL查询中选择数据来更新表

时间:2021-01-28 00:11:33

I have two tables

我有两张桌子

Order Table

订单表

or_id  count status
1        2    0
2        3    0
3        2    0

Order_Details Table

Order_Details表

ordetail         or_id    status
    1               1         1
    2               1         1
    3               2         0
    4               2         1
    5               2         1
    6               3         1
    7               3         1

I want update order table status to 1 if all the status of the corresponding order_id is 1 in Order_Details Table.I tried to work with this query, but it is not working since Subquery returns more than 1 row

如果在Order_Details表中相应的order_id的所有状态都是1,我希望更新订单表状态为1.我尝试使用此查询,但由于子查询返回超过1行,因此它无法工作

UPDATE order o JOIN order_detail od ON o.or_id = od.or_id SET o.Status = 1 WHERE 
o.or_id= (SELECT or_id FROM order_detail GROUP BY or_id 
HAVING SUM(status = 1) = COUNT(*) ) 

Thanks in advance

提前致谢

1 个解决方案

#1


3  

First query to update based on status:

首先根据状态更新查询:

UPDATE `order` o
  JOIN Order_Detail od ON o.or_id = od.order_id
SET o.Status = 1
WHERE od.Status = 1 

Second query to retrieve:

要检索的第二个查询:

SELECT DISTINCT order_id
FROM Order_Detail
WHERE status = 0

Note: If a order has 2 order_details, 1) with status = 0 and 2) status = 1. the above query will include that order - since there is a row with status = 0. If you want to only retrieve the order id's that are all status = 0. then use this query:

注意:如果订单有2个order_details,1)status = 0且2)status = 1.上面的查询将包含该订单 - 因为有一行status = 0.如果你只想检索订单id那个都是status = 0.然后使用此查询:

SELECT order_id
FROM Order_Detail
GROUP BY order_id
HAVING SUM(status = 0) = COUNT(*)

Update: As per comments, since you want to set status = 1 only if all the order details are 1, use this update query:

更新:根据评论,由于您只想在所有订单详细信息均为1时设置status = 1,请使用此更新查询:

 UPDATE `order` o
  JOIN (
    SELECT order_id
    FROM Order_Detail
    GROUP BY order_id
    HAVING SUM(status = 1) = COUNT(*)
  ) og ON o.or_id = og.order_id
SET o.Status = 1

#1


3  

First query to update based on status:

首先根据状态更新查询:

UPDATE `order` o
  JOIN Order_Detail od ON o.or_id = od.order_id
SET o.Status = 1
WHERE od.Status = 1 

Second query to retrieve:

要检索的第二个查询:

SELECT DISTINCT order_id
FROM Order_Detail
WHERE status = 0

Note: If a order has 2 order_details, 1) with status = 0 and 2) status = 1. the above query will include that order - since there is a row with status = 0. If you want to only retrieve the order id's that are all status = 0. then use this query:

注意:如果订单有2个order_details,1)status = 0且2)status = 1.上面的查询将包含该订单 - 因为有一行status = 0.如果你只想检索订单id那个都是status = 0.然后使用此查询:

SELECT order_id
FROM Order_Detail
GROUP BY order_id
HAVING SUM(status = 0) = COUNT(*)

Update: As per comments, since you want to set status = 1 only if all the order details are 1, use this update query:

更新:根据评论,由于您只想在所有订单详细信息均为1时设置status = 1,请使用此更新查询:

 UPDATE `order` o
  JOIN (
    SELECT order_id
    FROM Order_Detail
    GROUP BY order_id
    HAVING SUM(status = 1) = COUNT(*)
  ) og ON o.or_id = og.order_id
SET o.Status = 1