MYSQL通过子查询更新同表中的数据

一个问题折磨了很长的时间,今天终于解决了,效果很好,记录一下。

问题是这样的:有这样一张表

数据表
大类参数名数据1数据2数据3
S001A001张三2080
S001A002李四3085
S001A003王五2583
S001A004赵六2282
S001A005钱一2378
S002A001
S002A002
S002A003
S002A004
S002A005

现在想把这张表中的S002下面的A001-A005用S001下的A001-A005进行更新。

解决方法是:

循环执行,A001-A005

update 数据表 as newtable, 
    (select 数据1, 数据2, 数据3 from 数据表 where 大类='S001' and 参数名='A001') as oldtable 
    set newtable.数据1 = oldtable.数据1,
    set newtable.数据2 = oldtable.数据2,
    set newtable.数据3 = oldtable.数据3
where 大类='S002' and 参数名='A001'

用以上方法,完美高效实现。以下是我的实表代码:多条数据时,用程序循环执行就行。

UPDATE fieldsetup as newtable, (select chsname, unit, scale, LScope, UScope, effective, canControl, Address, functionNo, DataValue, oneshow, zeroshow, smsTime, smsContent, showChart, showAlert, showClose, showOpen, relatedAddress, sendSms, AILScope, AIUScope, cb32, showInDatalist, piror, averageLineFrequency from fieldsetup where systemshortname = 'shgqhb5001' AND fieldname = 'A001') as oldtable 
    SET newtable.chsname = oldtable.chsname, 
           newtable.unit = oldtable.unit, 
          newtable.scale = oldtable.scale, 
         newtable.LScope = oldtable.LScope, 
         newtable.UScope = oldtable.UScope, 
      newtable.effective = oldtable.effective, 
     newtable.canControl = oldtable.canControl, 
        newtable.Address = oldtable.Address, 
     newtable.functionNo = oldtable.functionNo, 
      newtable.DataValue = oldtable.DataValue, 
        newtable.oneshow = oldtable.oneshow, 
       newtable.zeroshow = oldtable.zeroshow, 
        newtable.smsTime = oldtable.smsTime, 
     newtable.smsContent = oldtable.smsContent, 
      newtable.showChart = oldtable.showChart, 
      newtable.showAlert = oldtable.showAlert, 
      newtable.showClose = oldtable.showClose, 
       newtable.showOpen = oldtable.showOpen, 
 newtable.relatedAddress = oldtable.relatedAddress, 
        newtable.sendSms = oldtable.sendSms, 
       newtable.AILScope = oldtable.AILScope, 
       newtable.AIUScope = oldtable.AIUScope, 
           newtable.cb32 = oldtable.cb32, 
 newtable.showInDatalist = oldtable.showInDatalist, 
          newtable.piror = oldtable.piror, 
newtable.averageLineFrequency = oldtable.averageLineFrequency 
WHERE systemshortname = 'shgqhb5002' AND fieldname = 'A001'

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pmmiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值