7

假设我必须插入一个有很多 fk 的表,只是为了在下面解释错误的语句

insert into mytable
values
(
somevalue
,somevalue
,select id from othertable1 where ...condition
,select id from othertable2 where ...condition
,select id from othertable3 where ...condition
)

所以基本上要插入的值来自不同的子查询,是否有可能实现这种行为?

4

2 回答 2

16
insert into mytable (columns)
select somevalue, somevalue, a.id, b.id, c.id
from
 othertable1 a
 cross join othertable2 b
 cross join othertable3 c
where
 a ... condition
 b ... condition
 c ... condition
于 2012-06-05T16:36:40.843 回答
1

您可以使用 select 语句进行插入吗?

INSERT INTO MYTABLE
SELECT (SOMEVALUE,
    SOMEVALUE,
    T1.ID,
    T2.ID
)
FROM ANOTHERTABLE T1
JOIN YETANOTHERTABLE T2
    ON T1.BLAH = T2.BLAH
WHERE condition1...
于 2012-06-05T16:39:58.257 回答