2016-11-05 76 views
0

我想使用另一个查询的输出将多行插入到我的表中。使用另一个查询的结果将多行插入到数据库中

我的INSERT语句是这样的:

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

请把它标记为正确的答案,如果它为你工作的感谢 –

相关问题