7

Lets suppose I've to insert into a table with many fk, just to explain here below the wrong statement:

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

so basically values to insert comes from different subqueries, is it possible to achieve such a behavior ?

0

2 Answers 2

18
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
Sign up to request clarification or add additional context in comments.

1 Comment

I have the same problem but with only two values and othertable1 and othertable2 are the same table: stackoverflow.com/questions/49025589/…
1

Can you use a select statement to do the insert?

INSERT INTO MYTABLE
SELECT (SOMEVALUE,
    SOMEVALUE,
    T1.ID,
    T2.ID
)
FROM ANOTHERTABLE T1
JOIN YETANOTHERTABLE T2
    ON T1.BLAH = T2.BLAH
WHERE condition1...

1 Comment

I can't use JOIN since ti is not correlated with T2, the cross join was the missing point for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.