我想插入一个新行,它复制原始行中的两个字段,并将最后一个字段更改为新值。这一切都在一张桌子上完成。
请原谅表名/字段,它们很长。
表1 - alert_template_allocations
表2 - io
我的尝试
insert into alert_template_allocations
(alert_template_allocation_io_id,
alert_template_allocation_alert_template_id,
alert_template_allocation_user_group_id)
values
(
(Select at.alert_template_allocation_io_id,
at.alert_template_allocation_alert_template_id
from alert_template_allocations at join io i on
i.io_id = at.alert_template_allocation_io_id
and i.io_station_id = 222)
, 4);
答案 0 :(得分:2)
使用INSERT INTO SELECT
语法:
INSERT INTO alert_template_allocations (alert_template_allocation_io_id,
alert_template_allocation_alert_template_id,
alert_template_allocation_user_group_id)
SELECT at.alert_template_allocation_io_id,
at.alert_template_allocation_alert_template_id,
4
FROM alert_template_allocations at
JOIN io i
ON i.io_id = at.alert_template_allocation_io_id
AND i.io_station_id = 222;