mysql automatically create 3 records in table when user creates 1 in another table -
i have table user enters records (table1) : table1
id name date desc amt 1 fred 11/30/2015 bridge 123 2 fred 11/30/2015 tunnel 234
i need parse through table1 , create 3 records in table2 (or if name/date/desc there, update amt field) : table2
id name date desc sortorder amt 3 fred 11/30/2015 bridge 1 123 4 fred 11/30/2015 bridge 2 123 5 fred 11/30/2015 bridge 3 123 6 fred 11/30/2015 tunnel 1 234 7 fred 11/30/2015 tunnel 2 234 8 fred 11/30/2015 tunnel 3 234
id in each table ai key. name , date indexed , foreign keys. efficient way this? thanks!
you insert records as:
insert table2(name, date, desc, sortorder amt) select t1.name, t1.date, t1.desc, n.n, t1.amt table1 t1 cross join (select 1 n union select 2 union select 3) n on duplicate key update amt = values(amt);
note: on duplicate key update
portion work, need unique index on whatever defines duplicate, perhaps:
create unique index unq_table1_4 on table1(name, date, desc, sortorder);
Comments
Post a Comment