mysql - Procedure to insert a data from one table to another table after calculations -
mysql - Procedure to insert a data from one table to another table after calculations -
i have 2 table- 1. user 2. widgets there one thousand of users in "user" table. , have created new table named "widgets". every user have 5 different widgets , need insert 5 entries "widgets" table each entry in "user" table. here detailed scenario-
"user" table have column -"id"
"widgets" table have columns- "user_id","widgetid","align","priority"
and each user.id want insert 5 records "widgets" table this-
1st id users table, 1,1,1
1st id users table, 2,2,2
1st id users table, 3,3,3
1st id users table, 4,4,4
1st id users table, 5,5,5
2nd id users table, 1,1,1
2nd id users table, 2,2,2
2nd id users table, 3,3,3
2nd id users table, 4,4,4
2nd id users table, 5,5,5 .................... ...............
i want create procedure doing same. please suggest me how can this.
i suggest separate table defaults. not allow defaults adapted business needs , offerings grow, allow template can used other purposes.
create table defaultwidgets (widgetid int, align int, priority int); insert defaultwidgets values (1,1,1); insert defaultwidgets values (2,2,2); insert defaultwidgets values (3,3,3); insert defaultwidgets values (4,4,4); insert defaultwidgets values (5,5,5); insert widgets values (1,1,100,100); -- demonstrate new row won't inserted user 1, widget 1. insert widgets select id, r.widgetid, r.align, r.priority users u cross bring together defaultwidgets r not exists ( select 1 widgets w userid = u.id , w.widgetid=r.widgetid );
mysql sql database stored-procedures
Comments
Post a Comment