如何使用内连接编写更新查询

Posted

技术标签:

【中文标题】如何使用内连接编写更新查询【英文标题】:How to write update query using Inner join 【发布时间】:2017-02-09 11:11:44 【问题描述】:

我有这个问题

Update ProductionDetails P Inner join 
(   SELECT Distinct A.BaseCode, A.BaseScale, (A.BaseScale * B.BasePer / 100)  AS BaseVal,  A.TreadCode, A.TreadScale, (A.TreadScale * B.TreadPer / 100) as TreadVal,  A.InterfaceCode, A.LipCode, A.LipScale,
    (A.LipScale * B.HsPer / 100) as LipVal, A.CenterCode, A.CenterScale, (A.CenterScale * B.CenterPer / 100) AS CenterVal,  A.StencilNo 
                  from productionDetails A  inner join 
                           BlendMaster B on (A.InterfaceCode = b.Category AND A.BaseCode = B.Base AND A.TreadCode = B.Tread AND A.centerCode = B.center)  
          Where B.Status = yes
) AS ResTable on ( P.StencilNo = ResTable.StencilNo ) 
        Set P.BaseValue = ResTable.BaseVal, P.TreadValue = ResTable.TreadVal , P.LipValue = ResTable.LipVal, P.CenterValue = ResTable.CenterVal

我需要更新 BaseValue、TreadValue、CenterValue、LipValue 字段上的 ProductionDetails 表,使用 中的计算值(SELECT Query ... ) AS ResTable .

如果我将命令写入 从 ResTable 中选择,我会得到值,但如果我更新它会显示错误“操作必须使用可更新查询”。我需要在 MS Access 2013

上运行它

这里是样本数据的查询

CREATE TABLE ProductionDetails(StencilNo Text, LipCode Text, LipScale Text,LipValue Number,BaseCode Text,BaseScale Number,BaseValue Number,InterfaceCode Text,CenterCode Text,CenterScale Number,CenterValue Number,TreadCode Text,TreadScale Number,TreadValue Number)

Create Table BlendMaster (Category Text, Base Text, BasePer number , HsPer number, Center text,  CenterPer number,  Tread text, TreadPer number)

Insert into ProductionDetails (StencilNo, LipCode , LipScale  ,BaseCode ,BaseScale , InterfaceCode ,CenterCode , CenterScale , TreadCode ,TreadScale  ) 
VALUES ('C160405234', '-', 0,'BFA10',48.44,'BF10+CEG28' , 'CEG28', 36.5, 'TRR51', 52.56)

Insert into BlendMaster (Category, Base, BasePer ,HsPer, Center , CenterPer , Tread,TreadPer  ) 
VALUES ('BF10+CEG28', 'BFA10',25, 25, 'CEG28', 15, 'TRR51', 18)

【问题讨论】:

【参考方案1】:

我认为更新应该这样完成

  UPDATE p 
SET    P.basevalue = ResTable.baseval, 
       P.treadvalue = ResTable.treadval, 
       P.lipvalue = ResTable.lipval, 
       P.centervalue = ResTable.centerval 
       from 
#productiondetails P 
       INNER JOIN (SELECT  A.basecode, 
                                   A.basescale, 
                                   ( A.basescale * B.baseper / 100 )     AS 
                                   BaseVal, 
                                   A.treadcode, 
                                   A.treadscale, 
                                   ( A.treadscale * B.treadper / 100 )   AS 
                                   TreadVal, 
                                   A.interfacecode, 
                                   A.lipcode, 
                                   A.lipscale, 
                                   ( A.lipscale * B.hsper / 100 )        AS 
                                   LipVal, 
                                   A.centercode, 
                                   A.centerscale, 
                                   ( A.centerscale * B.centerper / 100 ) AS 
                                   CenterVal, 
                                   A.stencilno 
                   FROM   #productiondetails A 
                          INNER JOIN #blendmaster B 
                                  ON  A.interfacecode = b.category 
                                       AND A.basecode = B.base  
                                       AND A.centercode = B.center  )
                   ---WHERE  B.status = 'yes' this condtiton is not present in yout 2 tables)
         ResTable 
               ON  P.stencilno = ResTable.stencilno 

【讨论】:

我试过这种方式,它也说“来自关键字”附近的语法错误 当我测试格式时,我运行另一个查询“Update ProductionDetails P inner Join BlendMaster B on (P.InterfaceCode = B.Category And P.TreadCode = B.Tread And P.CenterCode = B.Center ) 设置 P.BaseValue = 3.5 WHERE P.StencilNo='C141200864' " 结构同上,执行成功, @Bala 你的内部查询返回任何数据你可以检查一次 @Chanukya OP 说他想在 MS Access 2013 上运行该查询 赞成@chanukya,感谢您的时间和帮助。

以上是关于如何使用内连接编写更新查询的主要内容,如果未能解决你的问题,请参考以下文章

如何使用左连接和内连接编写 laravel 查询?

如何在 Linq EntityFramework 4 中使用内连接和除外

如何使用node.js控制sequelize中的内连接查询?

如何使用 Laravel 查询构建器编写嵌套连接?

如何通过对不同列使用可重用代码在 JDBC 中编写一个更新查询(更新 1 列)?

使用内连接代替子查询