通过从另一个表 MySQL 查询中选择数据来更新表
我有两个表
Order 表
or_id count status
1 2 0
2 3 0
3 2 0
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
如果 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(*) )
提前致谢
I have two tables
Order Table
or_id count status
1 2 0
2 3 0
3 2 0
Order_Details Table
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据状态更新的第一个查询:
要检索的第二个查询:
注意:如果订单有 2 个 order_details,1) 状态 = 0 且 2) 状态 = 1。上述查询将包含该订单 - 因为有一行状态为= 0。如果您只想检索状态 = 0 的订单 ID,则使用此查询:
更新:根据注释,由于您只想在所有订单详细信息均为 1 时设置状态 = 1,请使用此更新询问:
First query to update based on status:
Second query to retrieve:
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:
Update: As per comments, since you want to set status = 1 only if all the order details are 1, use this update query: