Rutger Huijsmans Rutger Huijsmans - 1 month ago 8
MySQL Question

Insert multiple rows into database using the results of another query

I want to insert multiple rows into my table using the output of another query.

My insert statement looks like this:

INSERT INTO group_messages (group_message_text,group_message_group_id,group_message_user_id)
VALUES ('Rounded ended! Hit the standings button in the top right corner to check your score.','$group_id','0')


Here
$group_id
will need to get filled with however many
group_ids
get returned from this query:

SELECT group_id
FROM groups


The second query now returns
3 group_ids: 1, 2, 3


This should thus lead to something like:

INSERT INTO group_messages (group_message_text,group_message_group_id,group_message_user_id)
VALUES ('Rounded ended! Hit the standings button in the top right corner to check your score.','1','0'), ('Rounded ended! Hit the standings button in the top right corner to check your score.','2','0'), ('Rounded ended! Hit the standings button in the top right corner to check your score.','3','0')

Answer

Try This

INSERT INTO group_messages 
(group_message_text,group_message_group_id,group_message_user_id)
select "Rounded ended! Hit the standings button in the top right corner to 
check your score.",group_id,0 FROM groups