Skip to content
Advertisement

Insert 2 select in same insert SQL

i have a problem, i would like use 2 select to insert in new table but i don’t know if it’s possible…

my code :

insert INTO attributs_libres_lignes VALUES  ( SELECT LIGN_PKID, "INSERT HERE" from ATTRIBUTS_LIGNES a, "HERE THE NUMBER OF COLUMN OF PKID (1 to 4)", "HERE A DATE");
select COLONNE01 from attributs_lignes
union all select COLONNE02 from attributs_lignes
union all select COLONNE03 from attributs_lignes
union all select COLONNE04 from attributs_lignes

I would like insert the second part ( select COLONNE01….) in the first part instead of “INSERT HERE”

DATA OLD TABLE : COLUMNS 1 (“PKID1″, one”,”two”,”three”) COLUMNS 2 (“PKID2″,”one”,”two”, “three”)

New TABLE :

(PKID1, one, 1) (PKID2, one, 1) (PKID1, two, 2) (PKID2, two, 2) (PKID1, three, 3) (PKID2, three, 3)

Advertisement

Answer

Isn’t that just as simple as this?

insert into attributs_libres_lignes
select lign_pkid, colonne01 from attributs_lignes union all
select lign_pkid, colonne02 from attributs_lignes union all
select lign_pkid, colonne03 from attributs_lignes union all
select lign_pkid, colonne04 from attributs_lignes;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement