用于更新密钥字段的SQL语句

时间:2014-09-15 14:12:26

标签: sql db2

我有两个表,pinfo和地址

PINFO:

pnumber  varchar 
myunikey INTEGER (unikey)
systemid integer

地址:

amyPinfoKey  INTEGER (new row with NULL val) 
anumber varchar (connects to to pinfo.pnumber)

(并非所有行都连接到pinfo表 - 某些行根本没有连接)

我正在尝试运行一个SQL命令,该命令仅在address.amyPinfoKey = pinfo.myunikey

的地方设置address.anumber = pinfo.pnumber

棘手的部分是pinfo.pnumber可以重复,所以我只需要pinfo.myunikey上的一个匹配(无论哪个匹配)

我正在使用DB2数据库

1 个答案:

答案 0 :(得分:0)

UDATE address
 SET    address.amyPinfoKey = (SELECT myunikey  = (SELECT myunikey
                          FROM   (SELECT MAX(myunikey) AS myunikeyMax,
                                         pnumber       AS pnumberMax
                                  FROM   pinfo
                                  GROUP  BY pinfo.pnumber) AS pinfoMax,
                                 pinfo
                          WHERE  pinfoMax.pnumberMax = pinfo.pnumber
                                 AND pinfoMax.myunikeyMax = pinfo.myunikey
                                 AND address.anumber = pinfo.pnumber)

希望能帮助你,这是一个独特的问题。 还有一些典型的更新语句

update tableA set tableA.ColA = (
                                   select tableB.colA
                                   from tableB
                                   where tableB.id = tableA.id
                                 --  and some other conditon 
                                )
where condition

并且像这样:

update (select tableA.colA,tableB.colA newA
        from tableA,tableB
        where tableA.id = tableB.id

    )
set colA = newA