Mysql Update with table joins - 用其他表字段的总和更新一个表的字段
我有两个表 Orders
和 Order_Details
Order_Details
表的 order_id
字段充当 Orders
表的 id_order
表的外键。
我想使用 Order_Details
表中的价格总和来更新 Orders
表的 price_total
字段。
我尝试使用以下查询但失败:-
Update Orders, Order_Details
SET Orders.price_total = sum(Order_Details.price)
WHERE Orders.price_total=0
GROUP BY Order_Details.id_order
错误 -
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY Order_Details.id_order' at line 4
如何在一个查询中执行此操作?
谢谢
I have two tables Orders
and Order_Details
Order_Details
tables's order_id
field acts as foreign key to Orders
table's id_order
table.
I want to update the price_total
field of Orders
table with summation of prices from Order_Details
table.
I tried with the following query but failed:-
Update Orders, Order_Details
SET Orders.price_total = sum(Order_Details.price)
WHERE Orders.price_total=0
GROUP BY Order_Details.id_order
Error -
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY Order_Details.id_order' at line 4
How to do it in one query?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其简化为
更新分组
You can simplify it to
updated for grouping