1

Topic: insert select

Kind day. There was a question concerning adding of the data from one table in another. There are 3 tables:
tab1:id, name, kod_id;
tab2:kod_id, length, line;
tab3:id, name, length, curdate;
Essence in that that it is necessary to add the data from the table tab2 in tab3 at which line = (for example "5"). the code:
The code:

' INSERT into tab3 (id, name, length, curdate) values (tab3_id.nextval, (select kode_id from tab2 where line=5), (select length from tab2 where line=5), sysdate)

It only would begin that it was easier to understand my problem actually the data from a field kod_i from tab2 is compared to value kod_id from tab1 and in tab3 goes any more kod_id and that value which corresponds in the table tab1
The code:

(select tab1.id from tab1 where tab1.kode_id = (select kode_id from tab2 where line = 5))

as a result in tab3.name there are values with tab1.id
It turns out from tab2 undertake field values kode_id at which line = 5, these values go through tab1 and tab1.id at which tab1.kode_id are equal tab2.kode_id go on changeover tab2.kode_id in tab3.name. But with it too all is clear. Made here so
The code:

(select tab1.id from tab1 where tab1.kode_id = (select kode_id from tab2 where line = 5))

. As a result there was here such request::::
The code:

INSERT into tab3 (id, name, length, curdate) values (tab3_id.nextval (select tab1.id from tab1 where tab1.kode_id = (select kode_id from tab2 where line = 5)), (select length from tab2 where line=5), sysdate).

And this request the single line at which line = 5 works only if in tab2 all. And if them more that quits naturally an error. Because at the first request "(select tab1.id from tab1 where tab1.kode_id = (select kode_id from tab2 where line = 5))" the request answer tab1.kode_id = [an array received at request]. A question as it to make... I Hope more less explained as as. Here porridge but to investigate I hope it turns out. It is necessary to make as though a request cycle.

2

Re: insert select

Begin with documentation reading on INSERT... SELECT Syntax . [spoiler] And generally forget about INSERT. VALUES is only a poor wrapper.

INSERT (fields)
VALUES (values1), (values2)...

it is equivalent

INSERT (fields)
SELECT (values1)
UNION ALL
SELECT (values2)
UNION ALL
...

but it is relieved of moronic subqueries. [/spoiler]

3

Re: insert select

ssm116;
DBMS what?
tab3_id.nextval Is more on Orakl similar, than on MySQL.