从特定行的另一个表中插入数据

时间:2016-11-14 01:51:19

标签: sql sql-server

我成功插入了其他表中的数据,但数据已插入到其他行中。如何指定将数据插入特定行的条件,大多数查询都具有来自其他(旧)表的条件。

INSERT INTO machine_types( machine_id)
    SELECT DISTINCT machine_id 
    FROM machine_events
    WHERE platform_id = '70ZOvy' AND cpu = 0.25 AND memory = 0.2498

machine_types表格有machine_type列,我希望在machine_type = 1上插入数据。

machine_types 表:

machine_id    // has null value now
cpu
memory
platform
machine_type

machine_events

machine_id
cpu
memory
platform

问题是我将如何编写SQL查询以将数据插入到具有machine_type = 1的记录中?

请注意。 machine_types表只有10条记录,而要插入的新记录是126条记录。所有这些记录都将转到machine_type = 1

Machine_types 表:

machine_type    platform    cpu     memory    machine_id
1                 HofLG     0.25    0.2498      NULL
2                 HofLG     0.5     0.03085     NULL
3                 HofLG     0.5     0.06158     NULL
4                 HofLG     0.5     0.1241      NULL

Machine_events 表:

time machine_id     platform_id  cpu    memory  machine_type
0      5             HofLG       0.25   0.2493     1
0      6             HofLG       0.5    0.03085    1
0      7             HofLG       0.5    0.2493     2
0      10            HofLG       0.5    0.2493     2

第一个表上的空值应该更改为基于machine_type从第二个表获取machine_id。

machine_id刚刚更新后

machine_types

    machine_type    platform    cpu     memory    machine_id
    1                 HofLG    0.25     0.2498      5
    1                 HofLG    0.5      0.03085     6
    2                 HofLG    0.5      0.03085     NULL
    3                 HofLG    0.5      0.06158     NULL
    4                 HofLG     0.5     0.1241      NULL

0 个答案:

没有答案
相关问题