我正在尝试使用子查询执行insert
,但此insert
失败:
insert into
TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
values (
1,
(select COL_B from TABLE_B where user_name = 'foo'),
(select COL_C from TABLE_C where age = 25),
2,3,4);
我试着写不同但它仍然失败。
答案 0 :(得分:6)
您需要insert . . . select
语法。不需要values
:
insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
select 1, (select COL_B from TABLE_B where user_name = 'foo'),
(select COL_C from TABLE_C where age = 25),
2, 3, 4;
如果您愿意,也可以将其写为cross join
:
insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
select 1, b.COL_B, c.COL_C,
2, 3, 4;
from (select COL_B from TABLE_B where user_name = 'foo') b cross join
(select COL_C from TABLE_C where age = 25) c