2016-11-05 56 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

請把它標記爲正確的答案,如果它爲你工作的感謝 –

相關問題