将表中的所有行插入到其他表中,单个查询 [英] insert all rows from table into other table, single query

查看:85
本文介绍了将表中的所有行插入到其他表中,单个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含user_ids的表,另一个包含user_ids和其他数据(空)的表.

I have a table with user_ids, and other table with user_ids and other data(empty).

我想将所有id插入到空表中.

I want to insert all the ids into the empty table.

我知道我可以使用php脚本甚至使用

I know I can just do it with i.e. php script or even create copy\paste script with

insert into tableB values(x),(y),(z);

我感兴趣的是,当我不提及每个值时,我是否可以使用sql来创建查询,更像是将所有选定的值复制到新表中. 喜欢

what I m interested in, though, is can i somehow use sql to create query when i dont mention each value and more like copy all selected values to new table. like

insert into tableB values(select x from tableA); --not SINGLE x! 
                                                 --ALL 'X' values returned in nested query...

推荐答案

看看st

插入...选择语法

使用INSERT ... SELECT,您可以快速将许多行插入到表中 来自一个或多个表.

With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables.

语法

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    SELECT ...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

示例

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

这篇关于将表中的所有行插入到其他表中,单个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆