mysql - inserting data into new table from union of two tables -


i have following query unions 2 tables table1 , table 2

resultset res = st.executequery(" select user_id, movie_id, movie_name, user_name, rating, genre table1 union select t1.user_id, t2.movie_id, t2.movie_name, t2.user_name,  t2.rating, t2.genre table2 t2 join table1 t1 on t2.user_name = t1.user_name;"); 

output--

1   12  pianist                 vishal  7   action 2   4   titanic                 rajesh  7       action 3   5   snakes on plane   anuj    2   drama 4   9   oh god           arun    5   drama 5   9   jumanji                 vishal  8   fantasy 6   68  rabbit hole         vishal  4   mystery 1   249 fast , furious    vishal  0   action 2   356 sun , horse           rajesh  0   fantasy 2   423 scream                  rajesh  0   comedy 2   391 alone                   rajesh  0   tragedy 2   739 price , pauper    rajesh  0   drama 4   451 7                   arun    5   comedy 5   9   ghosts                  vishal  0   horror 5   216 face off            vishal  0   comedy 5   344 future                  vishal  0   drama 5   387 night , day           vishal  0   suspense 5   249 fast , furious    vishal  0   action 6   9   ghosts                  vishal  0   horror 6   216 face off            vishal  0   comedy 6   344 future                  vishal  0   drama 6   387 night , day           vishal  0   suspense 6   249 fast , furious    vishal  0   action 

now want should insert values comming table 3 movie_id,user_id , rating

ie movie_id int user_id int rating varchar

ie

user_id movie_id    rating 1   12      7    2   4       7    3   5       2    4   9       5    5   9       8    6   68      4    1   249     0    2   356     0    2   423     0    2   391     0    2   739     0    4   451     5    5   9       0    5   216     0    5   344     0    5   387     0    5   249     0    6   9       0    6   216     0    6   344     0    6   387     0    6   249     0    

how write query following?

thanks

use insert ... select ...:

insert table3(movie_id, user_id, rating) select movie_id, user_id, rating (     select movie_id, user_id, rating     table1      union     select t2.movie_id, t1.user_id, t2.rating     table2 t2      join table1 t1 on t2.user_name = t1.user_name ) t; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -