Thursday, April 28, 2011

Insert one row for each key

Let's say I have two tables

lnglngdef:
lngId | lngName
1       Korean
2       Spanish

lngnottranslated:
lngId | engDef
1       Hello
2       Hello
1       Hi
2       Hi

Now I want to insert a new "engDef" with the value 'Welcome' in the lngnottranslated for each entry in the lnglngdef table. In this case that's two new rows in the lngnottranslated table with the lngId 1, 2 and the engDef set to 'welcome' on both entries. I've seen similar questions here but none really help me. Also I know this is not 100% following the normalization rules but this is the way it's implemented and is hard to change.

How would I do this in SQL (using mysql)?

From stackoverflow
  • Something like this?

    INSERT INTO lngnottranslated (lngid,engDef) SELECT lngid, 'Welcome' FROM lnglngdef;
    

    Here's the docs about INSERT SELECT

    Baversjo : Thank you very much! That worked, will read about INSERT SELECT :)

0 comments:

Post a Comment