【问题标题】:Mysql update using selectMysql 使用 select 更新
【发布时间】:2015-06-03 06:26:49
【问题描述】:

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

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

【问题讨论】:

  • 你的语法错误update table joins set where
  • 我像你说的一样使用了一些错误

标签: mysql sql sql-update


【解决方案1】:

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

【讨论】:

  • 感谢您的查询:) 我需要更新hedging 表并需要从orders cl 获取一些值查看我的问题和Try 1 查询我哪里出错了?
  • 你试过我的查询了吗?
  • 是的,出现错误#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 'where cur.Notation &lt;&gt; 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo
  • 我需要再澄清一下,检查我更新的问题,现在我像这样使用hed.OrderNo,所以对冲表列将被更新对吗?
  • 是的,您在SET 部分中指定的列都已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 2021-09-25
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多