一个问题折磨了很长的时间,今天终于解决了,效果很好,记录一下。
问题是这样的:有这样一张表
大类 | 参数名 | 数据1 | 数据2 | 数据3 |
---|---|---|---|---|
S001 | A001 | 张三 | 20 | 80 |
S001 | A002 | 李四 | 30 | 85 |
S001 | A003 | 王五 | 25 | 83 |
S001 | A004 | 赵六 | 22 | 82 |
S001 | A005 | 钱一 | 23 | 78 |
S002 | A001 | |||
S002 | A002 | |||
S002 | A003 | |||
S002 | A004 | |||
S002 | A005 |
现在想把这张表中的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'