使用另一个查询的结果将多行插入数据库

时间:2016-11-05 06:20:20

标签: mysql

我想使用另一个查询的输出在表中插入多行。

我的插入语句如下所示:

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')

此处$group_id需要填写,但此查询会返回多少group_ids个:

SELECT group_id
FROM groups

第二个查询现在返回3 group_ids: 1, 2, 3

这应该导致类似:

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')

1 个答案:

答案 0 :(得分:1)

试试这个

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