msyql insert into select
时间: 2023-08-23 15:15:49 浏览: 89
mysql中insert与select的嵌套使用方法
5星 · 资源好评率100%
INSERT INTO SELECT语句是MySQL中用于将查询结果插入到另一个表中的语法。语法格式如下:INSERT INTO table_name(column_list) SELECT select_list FROM another_table WHERE condition; \[1\]
例如,如果我们有一个名为sys_role_menu的表,其中包含menu_id和role_id两个字段,我们可以使用INSERT INTO SELECT语句将另一个表sys_role_menu中满足条件ru.role_id != 1的行插入到sys_role_menu表中,并将role_id设置为1。具体的语句如下:INSERT INTO sys_role_menu (menu_id, role_id) SELECT ru.menu_id, 1 AS role_id FROM sys_role_menu ru WHERE ru.role_id != 1; \[2\]
需要注意的是,当从同一个表中选择并插入时,MySQL会创建一个内部临时表来存放查询结果的行,然后将这些行插入到目标表中。但是,不能在同一语句中引用两次TEMPORARY表,因此不能使用INSERT INTO t ... SELECT ... FROM t的语法。\[3\]
#### 引用[.reference_title]
- *1* [MySQL INSERT INTO SELECT[猿教程]](https://blog.csdn.net/hj_5346/article/details/125708368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Mysql insert into + select 插入语句](https://blog.csdn.net/zsx1314lovezyf/article/details/124901109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [mysql insert into select使用方法详解](https://blog.csdn.net/weixin_39602976/article/details/113128287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文