使用select进行Mysql更新

时间:2015-06-03 06:26:50

标签: mysql sql sql-update

我正在尝试使用以下查询更新字段。以下查询中的错误是什么?

  

MySQL说:文档

     

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   在第2行'FROM orders cl'附近

update hedging SET 
        Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3)),
        OrderNo = cl.OrderNo,Client = cn.ShortName,
        Currency = cur.Notation, SellingAmount = pto.PIAmount , 
        BuyingAmount = pto.POAmount 
        FROM  orders cl
        left join client cn on cl.ClientID = cn.ClientID 
        inner join department dpt on cl.DeptID = dpt.DeptID
        inner join supplier sp on cl.SupplierID = sp.SupplierId 
        left join staff st on st.StaffID = cl.SalesPerson
        left join pipo_total pto on pto.OrderNo = cl.OrderNo
        inner join groups grp on cl.GroupID = grp.GroupID
        left join currency cur on cur.CurrencyID= cl.SellCurrencyID  
        left join hedging hed on hed.OrderNo = cl.OrderNo) 
        where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo

尝试1

update orders cl 
        left join client cn on cl.ClientID = cn.ClientID 
        inner join department dpt on cl.DeptID = dpt.DeptID
        inner join supplier sp on cl.SupplierID = sp.SupplierId 
        left join staff st on st.StaffID = cl.SalesPerson
        left join pipo_total pto on pto.OrderNo = cl.OrderNo
        inner join groups grp on cl.GroupID = grp.GroupID
        left join currency cur on cur.CurrencyID= cl.SellCurrencyID  
        left join hedging hed on hed.OrderNo = cl.OrderNo
SET hed.Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3))
    ,hed.OrderNo = cl.OrderNo
    ,hed.Client = cn.ShortName
    ,hed.Currency = cur.Notation
    ,hed.SellingAmount = pto.PIAmount
    ,hed.BuyingAmount = pto.POAmount
where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo

1 个答案:

答案 0 :(得分:2)

带有多个表的

UPDATE应该是这样的

UPDATE  table1 t1
        INNER JOIN table2 t2 ON t1.ID = t2.ID
SET     t1.value = [value]

修改:您更新的查询

update  orders cl
        left join client cn on cl.ClientID = cn.ClientID 
        inner join department dpt on cl.DeptID = dpt.DeptID
        inner join supplier sp on cl.SupplierID = sp.SupplierId 
        left join staff st on st.StaffID = cl.SalesPerson
        left join pipo_total pto on pto.OrderNo = cl.OrderNo
        inner join groups grp on cl.GroupID = grp.GroupID
        left join currency cur on cur.CurrencyID= cl.SellCurrencyID  
        left join hedging hed on hed.OrderNo = cl.OrderNo) 
SET Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3))
    ,OrderNo = cl.OrderNo
    ,Client = cn.ShortName
    ,Currency = cur.Notation
    ,SellingAmount = pto.PIAmount
    ,BuyingAmount = pto.POAmount
where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo