通过连接3个表来更新表

时间:2015-04-08 20:06:40

标签: mysql

我有3张桌子:

  • tblproduct(pro_Id,qty,unitprice)
  • tblorderdetails(order_id,pro_Id,qty)
  • tblorder(order_id,totalAmount)

我打算加入这些表,以更新 tblorder表中的 totalAmount 。这是我使用MySql控制台的查询:

UPDATE o
SET o.totalAmount = p.unitprice * d.qty
FROM tblorder o INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id;

这是我得到的错误:

ERROR 1064 (42000): 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 'from tblorder o inner join tblorderdetails d

1 个答案:

答案 0 :(得分:0)

语法不正确,应该是

UPDATE  tblorder o
INNER JOIN tblorderdetails d
on o.order_id = d.order_id
INNER JOIN tblproduct p
on p.pro_Id = d.pro_Id
SET o.totalAmount = p.unitprice * d.qty ;