【问题标题】:Insert multiple rows into database using the results of another query [duplicate]使用另一个查询的结果将多行插入数据库[重复]
【发布时间】:2017-03-19 00:54:06
【问题描述】:

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

我的插入语句如下所示:

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

【问题讨论】:

  • 可以使用插入选择命令

标签: mysql


【解决方案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

【讨论】:

  • 如果对你有用,请标记为正确答案,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-11-14
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多