使用ID更新空值单元格,该表与其他3个相关

时间:2018-02-09 10:04:25

标签: sql oracle

我有一个从其他表连接的表。我想通过特定的数字系列更新此表中的空值。以下是插图:

Table的代码,名为List_Codes

SELECT mlk.MLK_CODE
 FROM zpt
 LEFT OUTER JOIN mes ON mes.ZPT_ID = zpt.ZPT_ID 
 LEFT OUTER JOIN zmlk ON zpt.ZPT_ID = zmlk.ZPT_ID 
 LEFT OUTER JOIN mlk ON zmlk.MLK_ID = mlk.MLK_ID
 WHERE zpt.zpt_id IS NOT NULL 
 and zpt.zpt_meteringcode = '123456'
 ORDER BY mes.MES_STATUS DESC 

现在我想将此特定行的mlk.MLK_CODE从null更新为' 789'。我根据zpt.zpt_meteringcode找到了这一行。有什么建议吗? 表格如下所示,List_Code表格是上述代码的结果

Mlk Table
  Mlk_id Mlk_code
    1   123
    2   456

Zpt Table
Zpt_id  Zpt_meteringcode
10       123456
20       987654
30       654321
40       147852
Zmlk Table
Zpt_id  Mlk_id
20        1
30        2

List_Code Table
Zpt_id  Zpt_meteringcode    Mlk_id  Mlk_code
10       123456     
20       987654                1    123
30       654321                2    456
40       147852     

1 个答案:

答案 0 :(得分:0)

我认为你需要两个插件和一个这样的更新语句。

INSERT INTO mlk (mlk_code)
    VALUES ( '789' ); -- Primary key is generated

.....

INSERT INTO zmlk  (zpt_id,
                     mlk_id)
        SELECT (SELECT zpt_id
                FROM   zpt
                WHERE  zpt_meteringcode = '123456'),
               (SELECT mlk_id
                FROM   mlk
                WHERE  mlk_code = '789')
        FROM   dual;

....

UPDATE list_code
    SET    ( mlk_id, mlk_code ) = (SELECT mlk_id,
                                          mlk_code
                                   FROM   mlk
                                   WHERE  mlk_code = '789')
    WHERE  zpt_meteringcode = '123456';