mysql - How to insert data into relation table from two queries -


wasn't sure how write question.

i have these 3 tables:

pictures id name  materials id name  pictures_materials picture_id material_id 

i have thousands of pictures , 5 materials , of pictures have materials. using query i've found out 100 pictures don't have material.

select p.id, p.name pictures p left outer join pictures_materials pm on p.id = pm.picture_id pm.picture_id null; 

what insert pictures_materials row each of these pictures each material. so, let's these of pictures no materials:

picture_21, picture_22, picture_23 

i want have:

picture_21 material_1 picture_21 material_2 picture_21 material_3 picture_21 material_4 ... 

is possible in single insert query?

is possible in single insert query?

yes!

you have found 1 legitimate reason create cartesian product. use cross join. try query.

-- insert picture_materials select dt.id, m.id  (select p.id pictures p left outer join pictures_materials pm on p.id = pm.picture_id pm.picture_id null) dt  cross join materials 

after validating select statement returns desired data, uncomment insert statement , execute again.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -