插入...值(SELECT ... FROM ...)

发布于 2024-07-04 12:32:10 字数 649 浏览 18 评论 0原文

我正在尝试使用另一个表的输入INSERT INTO一个表。 尽管这对于许多数据库引擎来说是完全可行的,但我似乎总是很难记住当今 SQL 引擎的正确语法(MySQLOracleSQL ServerInformixDB2)。

是否有来自 SQL 标准的银弹语法(例如 SQL-92 )这将允许我插入值而不用担心底层数据库?

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle to remember the correct syntax for the SQL engine of the day (MySQL, Oracle, SQL Server, Informix, and DB2).

Is there a silver-bullet syntax coming from an SQL standard (for example, SQL-92) that would allow me to insert the values without worrying about the underlying database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

等待我真够勒 2024-07-11 12:32:10

Postgres 支持以下功能:
创建表company.monitor2作为select * from company.monitor;

Postgres supports next:
create table company.monitor2 as select * from company.monitor;

白鸥掠海 2024-07-11 12:32:10

如果你首先创建表,你可以这样使用;

  select * INTO TableYedek From Table

这会插入值,但与创建新的复制表不同。

If you create table firstly you can use like this;

  select * INTO TableYedek From Table

This metot insert values but differently with creating new copy table.

深海夜未眠 2024-07-11 12:32:10

在 informix 中,它的工作方式正如 Claude 所说:

INSERT INTO table (column1, column2) 
VALUES (value1, value2);    

In informix it works as Claude said:

INSERT INTO table (column1, column2) 
VALUES (value1, value2);    
驱逐舰岛风号 2024-07-11 12:32:10

如果您使用 INSERT VALUES 路径插入多行,请确保使用括号将 VALUES 分隔到集合中,这样:

INSERT INTO `receiving_table`
  (id,
  first_name,
  last_name)
VALUES 
  (1002,'Charles','Babbage'),
  (1003,'George', 'Boole'),
  (1001,'Donald','Chamberlin'),
  (1004,'Alan','Turing'),
  (1005,'My','Widenius');

否则 MySQL 会认为“列计数与第 1 行的值计数不匹配”,并且您最终会编写一个简单的代码当您最终弄清楚该怎么做时,请发布。

If you go the INSERT VALUES route to insert multiple rows, make sure to delimit the VALUES into sets using parentheses, so:

INSERT INTO `receiving_table`
  (id,
  first_name,
  last_name)
VALUES 
  (1002,'Charles','Babbage'),
  (1003,'George', 'Boole'),
  (1001,'Donald','Chamberlin'),
  (1004,'Alan','Turing'),
  (1005,'My','Widenius');

Otherwise MySQL objects that "Column count doesn't match value count at row 1", and you end up writing a trivial post when you finally figure out what to do about it.

羅雙樹 2024-07-11 12:32:10

这是使用多个表获取源的另一个示例:

INSERT INTO cesc_pf_stmt_ext_wrk( 
  PF_EMP_CODE    ,
  PF_DEPT_CODE   ,
  PF_SEC_CODE    ,
  PF_PROL_NO     ,
  PF_FM_SEQ      ,
  PF_SEQ_NO      ,
  PF_SEP_TAG     ,
  PF_SOURCE) 
SELECT
  PFl_EMP_CODE    ,
  PFl_DEPT_CODE   ,
  PFl_SEC         ,
  PFl_PROL_NO     ,
  PF_FM_SEQ       ,
  PF_SEQ_NO       ,
  PFl_SEP_TAG     ,
  PF_SOURCE
 FROM cesc_pf_stmt_ext,
      cesc_pfl_emp_master
 WHERE pfl_sep_tag LIKE '0'
   AND pfl_emp_code=pf_emp_code(+);

COMMIT;

Here is another example where source is taken using more than one table:

INSERT INTO cesc_pf_stmt_ext_wrk( 
  PF_EMP_CODE    ,
  PF_DEPT_CODE   ,
  PF_SEC_CODE    ,
  PF_PROL_NO     ,
  PF_FM_SEQ      ,
  PF_SEQ_NO      ,
  PF_SEP_TAG     ,
  PF_SOURCE) 
SELECT
  PFl_EMP_CODE    ,
  PFl_DEPT_CODE   ,
  PFl_SEC         ,
  PFl_PROL_NO     ,
  PF_FM_SEQ       ,
  PF_SEQ_NO       ,
  PFl_SEP_TAG     ,
  PF_SOURCE
 FROM cesc_pf_stmt_ext,
      cesc_pfl_emp_master
 WHERE pfl_sep_tag LIKE '0'
   AND pfl_emp_code=pf_emp_code(+);

COMMIT;
蓝色星空 2024-07-11 12:32:10

以下是从多个表插入的方法。 在这个特定的例子中,您在多对多场景中拥有一个映射表:(

insert into StudentCourseMap (StudentId, CourseId) 
SELECT  Student.Id, Course.Id FROM Student, Course 
WHERE Student.Name = 'Paddy Murphy' AND Course.Name = 'Basket weaving for beginners'

我意识到匹配学生姓名可能会返回多个值,但您明白了。当 ID 为身份列并且未知。)

Here's how to insert from multiple tables. This particular example is where you have a mapping table in a many to many scenario:

insert into StudentCourseMap (StudentId, CourseId) 
SELECT  Student.Id, Course.Id FROM Student, Course 
WHERE Student.Name = 'Paddy Murphy' AND Course.Name = 'Basket weaving for beginners'

(I realise matching on the student name might return more than one value but you get the idea. Matching on something other than an Id is necessary when the Id is an Identity column and is unknown.)

始终不够 2024-07-11 12:32:10

如果您想使用 SELECT * INTO 表插入所有列,您可以尝试此操作。

SELECT  *
INTO    Table2
FROM    Table1;

You could try this if you want to insert all column using SELECT * INTO table.

SELECT  *
INTO    Table2
FROM    Table1;
月亮坠入山谷 2024-07-11 12:32:10

实际上我更喜欢 SQL Server 2008 中的以下功能:

SELECT Table1.Column1, Table1.Column2, Table2.Column1, Table2.Column2, 'Some String' AS SomeString, 8 AS SomeInt
INTO Table3
FROM Table1 INNER JOIN Table2 ON Table1.Column1 = Table2.Column3

它消除了添加 Insert() 集的步骤,您只需选择将哪些值放入表中即可。

I actually prefer the following in SQL Server 2008:

SELECT Table1.Column1, Table1.Column2, Table2.Column1, Table2.Column2, 'Some String' AS SomeString, 8 AS SomeInt
INTO Table3
FROM Table1 INNER JOIN Table2 ON Table1.Column1 = Table2.Column3

It eliminates the step of adding the Insert () set, and you just select which values go in the table.

你的背包 2024-07-11 12:32:10

这对我有用:

insert into table1 select * from table2

这句话与 Oracle 的有点不同。

This worked for me:

insert into table1 select * from table2

The sentence is a bit different from Oracle's.

悲喜皆因你 2024-07-11 12:32:10
INSERT INTO yourtable
SELECT fielda, fieldb, fieldc
FROM donortable;

这适用于所有 DBMS

INSERT INTO yourtable
SELECT fielda, fieldb, fieldc
FROM donortable;

This works on all DBMS

旧梦荧光笔 2024-07-11 12:32:10

对于 Microsoft SQL Server,我建议学习解释 MSDN 上提供的语法。 有了 Google,查找语法比以往任何时候都容易。

对于这种特殊情况,请尝试

Google:插入网站:microsoft.com

第一个结果将是 http://msdn。 microsoft.com/en-us/library/ms174335.aspx

如果您发现很难解释给出的语法,请向下滚动到 示例(“使用 SELECT 和 EXECUTE 选项从其他表插入数据”)页面顶部。

[ WITH <common_table_expression> [ ,...n ] ]
INSERT 
{
        [ TOP ( expression ) [ PERCENT ] ] 
        [ INTO ] 
        { <object> | rowset_function_limited 
          [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
        }
    {
        [ ( column_list ) ] 
        [ <OUTPUT Clause> ]
        { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
        | derived_table       <<<<------- Look here ------------------------
        | execute_statement   <<<<------- Look here ------------------------
        | <dml_table_source>  <<<<------- Look here ------------------------
        | DEFAULT VALUES 
        }
    }
}
[;]

这应该适用于任何其他可用的 RDBMS。 在我看来,记住所有产品的所有语法是没有意义的。

For Microsoft SQL Server, I will recommend learning to interpret the SYNTAX provided on MSDN. With Google it's easier than ever, to look for syntax.

For this particular case, try

Google: insert site:microsoft.com

The first result will be http://msdn.microsoft.com/en-us/library/ms174335.aspx

scroll down to the example ("Using the SELECT and EXECUTE options to insert data from other tables") if you find it difficult to interpret the syntax given at the top of the page.

[ WITH <common_table_expression> [ ,...n ] ]
INSERT 
{
        [ TOP ( expression ) [ PERCENT ] ] 
        [ INTO ] 
        { <object> | rowset_function_limited 
          [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
        }
    {
        [ ( column_list ) ] 
        [ <OUTPUT Clause> ]
        { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
        | derived_table       <<<<------- Look here ------------------------
        | execute_statement   <<<<------- Look here ------------------------
        | <dml_table_source>  <<<<------- Look here ------------------------
        | DEFAULT VALUES 
        }
    }
}
[;]

This should be applicable for any other RDBMS available there. There is no point in remembering all the syntax for all products IMO.

扭转时空 2024-07-11 12:32:10
INSERT INTO FIRST_TABLE_NAME (COLUMN_NAME)
SELECT  COLUMN_NAME
FROM    ANOTHER_TABLE_NAME 
WHERE CONDITION;
INSERT INTO FIRST_TABLE_NAME (COLUMN_NAME)
SELECT  COLUMN_NAME
FROM    ANOTHER_TABLE_NAME 
WHERE CONDITION;
静水深流 2024-07-11 12:32:10

从任何其他表插入多条记录的最佳方法。

INSERT  INTO dbo.Users
            ( UserID ,
              Full_Name ,
              Login_Name ,
              Password
            )
            SELECT  UserID ,
                    Full_Name ,
                    Login_Name ,
                    Password
            FROM    Users_Table
            (INNER JOIN / LEFT JOIN ...)
            (WHERE CONDITION...)
            (OTHER CLAUSE)

Best way to insert multiple records from any other tables.

INSERT  INTO dbo.Users
            ( UserID ,
              Full_Name ,
              Login_Name ,
              Password
            )
            SELECT  UserID ,
                    Full_Name ,
                    Login_Name ,
                    Password
            FROM    Users_Table
            (INNER JOIN / LEFT JOIN ...)
            (WHERE CONDITION...)
            (OTHER CLAUSE)
傲影 2024-07-11 12:32:10
select *
into tmp
from orders

看起来不错,但仅当 tmp 不存在时才有效(创建它并填充)。 (SQL 服务器)

插入现有的 tmp 表:

set identity_insert tmp on

insert tmp 
([OrderID]
      ,[CustomerID]
      ,[EmployeeID]
      ,[OrderDate]
      ,[RequiredDate]
      ,[ShippedDate]
      ,[ShipVia]
      ,[Freight]
      ,[ShipName]
      ,[ShipAddress]
      ,[ShipCity]
      ,[ShipRegion]
      ,[ShipPostalCode]
      ,[ShipCountry] )
      select * from orders

set identity_insert tmp off
select *
into tmp
from orders

Looks nice, but works only if tmp doesn't exists (creates it and fills). (SQL sever)

To insert into existing tmp table:

set identity_insert tmp on

insert tmp 
([OrderID]
      ,[CustomerID]
      ,[EmployeeID]
      ,[OrderDate]
      ,[RequiredDate]
      ,[ShippedDate]
      ,[ShipVia]
      ,[Freight]
      ,[ShipName]
      ,[ShipAddress]
      ,[ShipCity]
      ,[ShipRegion]
      ,[ShipPostalCode]
      ,[ShipCountry] )
      select * from orders

set identity_insert tmp off
镜花水月 2024-07-11 12:32:10

如果你想向表中插入一些数据而不想写列名。

INSERT INTO CUSTOMER_INFO
   (SELECT CUSTOMER_NAME,
           MOBILE_NO,
           ADDRESS
      FROM OWNER_INFO cm)

表格所在位置:

            CUSTOMER_INFO               ||            OWNER_INFO
----------------------------------------||-------------------------------------
CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
--------------|-----------|---------    || --------------|-----------|--------- 
      A       |     +1    |   DC        ||       B       |     +55   |   RR  

结果:

            CUSTOMER_INFO               ||            OWNER_INFO
----------------------------------------||-------------------------------------
CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
--------------|-----------|---------    || --------------|-----------|--------- 
      A       |     +1    |   DC        ||       B       |     +55   |   RR
      B       |     +55   |   RR        ||

IF you want to insert some data into a table without want to write column name.

INSERT INTO CUSTOMER_INFO
   (SELECT CUSTOMER_NAME,
           MOBILE_NO,
           ADDRESS
      FROM OWNER_INFO cm)

Where the tables are:

            CUSTOMER_INFO               ||            OWNER_INFO
----------------------------------------||-------------------------------------
CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
--------------|-----------|---------    || --------------|-----------|--------- 
      A       |     +1    |   DC        ||       B       |     +55   |   RR  

Result:

            CUSTOMER_INFO               ||            OWNER_INFO
----------------------------------------||-------------------------------------
CUSTOMER_NAME | MOBILE_NO | ADDRESS     || CUSTOMER_NAME | MOBILE_NO | ADDRESS 
--------------|-----------|---------    || --------------|-----------|--------- 
      A       |     +1    |   DC        ||       B       |     +55   |   RR
      B       |     +55   |   RR        ||
梦里°也失望 2024-07-11 12:32:10

如果您要为 SELECT 部分中的所有列提供值,则无需在 INSERT INTO 部分中指定列即可完成此操作。

假设 table1 有两列。 此查询应该有效:

INSERT INTO table1
SELECT  col1, col2
FROM    table2

这不起作用(未指定 col2 的值):

INSERT INTO table1
SELECT  col1
FROM    table2

我正在使用 MS SQL Server。 我不知道其他 RDMS 是如何工作的。

This can be done without specifying the columns in the INSERT INTO part if you are supplying values for all columns in the SELECT part.

Let's say table1 has two columns. This query should work:

INSERT INTO table1
SELECT  col1, col2
FROM    table2

This WOULD NOT work (value for col2 is not specified):

INSERT INTO table1
SELECT  col1
FROM    table2

I'm using MS SQL Server. I don't know how other RDMS work.

空城旧梦 2024-07-11 12:32:10

只需在 INSERT 中使用括号将 SELECT 子句插入即可。 例如这样:

INSERT INTO Table1 (col1, col2, your_desired_value_from_select_clause, col3)
VALUES (
   'col1_value', 
   'col2_value',
   (SELECT col_Table2 FROM Table2 WHERE IdTable2 = 'your_satisfied_value_for_col_Table2_selected'),
   'col3_value'
);

Just use parenthesis for SELECT clause into INSERT. For example like this :

INSERT INTO Table1 (col1, col2, your_desired_value_from_select_clause, col3)
VALUES (
   'col1_value', 
   'col2_value',
   (SELECT col_Table2 FROM Table2 WHERE IdTable2 = 'your_satisfied_value_for_col_Table2_selected'),
   'col3_value'
);
深居我梦 2024-07-11 12:32:10

这是使用带有 select 的值的另一个示例:

INSERT INTO table1(desc, id, email) 
SELECT "Hello World", 3, email FROM table2 WHERE ...

This is another example using values with select:

INSERT INTO table1(desc, id, email) 
SELECT "Hello World", 3, email FROM table2 WHERE ...
随遇而安 2024-07-11 12:32:10

已知表列顺序时的简单插入:

    Insert into Table1
    values(1,2,...)

提及列的简单插入:

    Insert into Table1(col2,col4)
    values(1,2)

当表(#table2)的选定列数等于插入表(Table1)时的批量插入

    Insert into Table1 {Column sequence}
    Select * -- column sequence should be same.
       from #table2

当您只想插入表中所需的列时,批量插入(表格1):

    Insert into Table1 (Column1,Column2 ....Desired Column from Table1)  
    Select Column1,Column2..desired column from #table2
       from #table2

Simple insertion when table column sequence is known:

    Insert into Table1
    values(1,2,...)

Simple insertion mentioning column:

    Insert into Table1(col2,col4)
    values(1,2)

Bulk insertion when number of selected columns of a table(#table2) are equal to insertion table(Table1)

    Insert into Table1 {Column sequence}
    Select * -- column sequence should be same.
       from #table2

Bulk insertion when you want to insert only into desired column of a table(table1):

    Insert into Table1 (Column1,Column2 ....Desired Column from Table1)  
    Select Column1,Column2..desired column from #table2
       from #table2
新人笑 2024-07-11 12:32:10

大多数数据库都遵循基本语法,

INSERT INTO TABLE_NAME
SELECT COL1, COL2 ...
FROM TABLE_YOU_NEED_TO_TAKE_FROM
;

我使用过的每个数据库都遵循这种语法,即 DB2SQL ServerMY SQLPostgresQL

Most of the databases follow the basic syntax,

INSERT INTO TABLE_NAME
SELECT COL1, COL2 ...
FROM TABLE_YOU_NEED_TO_TAKE_FROM
;

Every database I have used follow this syntax namely, DB2, SQL Server, MY SQL, PostgresQL

ま柒月 2024-07-11 12:32:10

为了从另一个表中获取多值 INSERT 中的一个值,我在 SQLite3 中执行了以下操作:

INSERT INTO column_1 ( val_1, val_from_other_table ) 
VALUES('val_1', (SELECT  val_2 FROM table_2 WHERE val_2 = something))

To get only one value in a multi value INSERT from another table I did the following in SQLite3:

INSERT INTO column_1 ( val_1, val_from_other_table ) 
VALUES('val_1', (SELECT  val_2 FROM table_2 WHERE val_2 = something))
何以畏孤独 2024-07-11 12:32:10

我看到的两个答案在 Informix 中都工作得很好,并且基本上都是标准 SQL。 也就是说,符号:

INSERT INTO target_table[(<column-list>)] SELECT ... FROM ...;

可以很好地与 Informix 配合使用,并且我希望与所有 DBMS 配合使用。 (在 5 年前或更多年前,MySQL 并不总是支持这种事情;现在它对这种标准 SQL 语法提供了很好的支持,而且,据我所知,它在这种表示法上可以正常工作。)是可选的,但按顺序指示目标列,因此 SELECT 结果的第一列将进入第一个列出的列,依此类推。在没有列列表的情况下,SELECT 结果的第一列进入目标表的第一列。

系统之间的不同之处在于用于标识不同数据库中的表的符号 - 该标准没有提及数据库间(更不用说 DBMS 间)操作。 使用 Informix,您可以使用以下表示法来标识表:

[dbase[@server]:][owner.]table

也就是说,您可以指定一个数据库,如果该数据库不在当前服务器中,则可以选择标识托管该数据库的服务器,后跟可选的所有者、点,最后实际的表名。 SQL 标准使用术语模式来表示 Informix 所称的所有者。 因此,在 Informix 中,以下任何符号都可以标识表:

table
"owner".table
dbase:table
dbase:owner.table
dbase@server:table
dbase@server:owner.table

所有者通常不需要加引号; 但是,如果您确实使用引号,则需要正确拼写所有者名称 - 它区分大小写。 即:

someone.table
"someone".table
SOMEONE.table

都标识同一个表。 对于 Informix,MODE ANSI 数据库有一个轻微的复杂性,其中所有者名称通常转换为大写(informix 是例外)。 也就是说,在 MODE ANSI 数据库(不常用)中,您可以编写:

CREATE TABLE someone.table ( ... )

并且系统目录中的所有者名称将是“SOMEONE”,而不是“someone”。 如果将所有者名称括在双引号中,则它的作用类似于分隔标识符。 对于标准 SQL,分隔标识符可以在很多地方使用。 使用 Informix,您只能在所有者名称周围使用它们——在其他上下文中,Informix 将单引号和双引号字符串都视为字符串,而不是将单引号字符串视为字符串,将双引号字符串视为分隔标识符。 (当然,为了完整起见,有一个环境变量 DELIMIDENT,可以设置为任何值,但 Y 是最安全的 - 指示双引号始终包围定界标识符,单引号始终包围字符串。)

请注意,MS SQL Server 设法使用方括号括起来的[分隔标识符]。 它对我来说看起来很奇怪,并且肯定不是 SQL 标准的一部分。

Both the answers I see work fine in Informix specifically, and are basically standard SQL. That is, the notation:

INSERT INTO target_table[(<column-list>)] SELECT ... FROM ...;

works fine with Informix and, I would expect, all the DBMS. (Once upon 5 or more years ago, this is the sort of thing that MySQL did not always support; it now has decent support for this sort of standard SQL syntax and, AFAIK, it would work OK on this notation.) The column list is optional but indicates the target columns in sequence, so the first column of the result of the SELECT will go into the first listed column, etc. In the absence of the column list, the first column of the result of the SELECT goes into the first column of the target table.

What can be different between systems is the notation used to identify tables in different databases - the standard has nothing to say about inter-database (let alone inter-DBMS) operations. With Informix, you can use the following notation to identify a table:

[dbase[@server]:][owner.]table

That is, you may specify a database, optionally identifying the server that hosts that database if it is not in the current server, followed by an optional owner, dot, and finally the actual table name. The SQL standard uses the term schema for what Informix calls the owner. Thus, in Informix, any of the following notations could identify a table:

table
"owner".table
dbase:table
dbase:owner.table
dbase@server:table
dbase@server:owner.table

The owner in general does not need to be quoted; however, if you do use quotes, you need to get the owner name spelled correctly - it becomes case-sensitive. That is:

someone.table
"someone".table
SOMEONE.table

all identify the same table. With Informix, there's a mild complication with MODE ANSI databases, where owner names are generally converted to upper-case (informix is the exception). That is, in a MODE ANSI database (not commonly used), you could write:

CREATE TABLE someone.table ( ... )

and the owner name in the system catalog would be "SOMEONE", rather than 'someone'. If you enclose the owner name in double quotes, it acts like a delimited identifier. With standard SQL, delimited identifiers can be used many places. With Informix, you can use them only around owner names -- in other contexts, Informix treats both single-quoted and double-quoted strings as strings, rather than separating single-quoted strings as strings and double-quoted strings as delimited identifiers. (Of course, just for completeness, there is an environment variable, DELIMIDENT, that can be set - to any value, but Y is safest - to indicate that double quotes always surround delimited identifiers and single quotes always surround strings.)

Note that MS SQL Server manages to use [delimited identifiers] enclosed in square brackets. It looks weird to me, and is certainly not part of the SQL standard.

暮凉 2024-07-11 12:32:10

尝试:

INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2  

这是标准 ANSI SQL,应该适用于任何 DBMS

它绝对适用于:

  • Oracle
  • MS SQL Server
  • MySQL
  • Postgres
  • SQLite v3
  • Teradata
  • DB2
  • Sybase
  • Vertica
  • HSQLDB
  • H2
  • AWS RedShift
  • SAP HANA
  • Google Spanner

Try:

INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2  

This is standard ANSI SQL and should work on any DBMS

It definitely works for:

  • Oracle
  • MS SQL Server
  • MySQL
  • Postgres
  • SQLite v3
  • Teradata
  • DB2
  • Sybase
  • Vertica
  • HSQLDB
  • H2
  • AWS RedShift
  • SAP HANA
  • Google Spanner
笑咖 2024-07-11 12:32:10

Claude Houle 的答案:应该可以正常工作,而且您也可以拥有多个列和其他数据:

INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT  table2.column1, table2.column2, 8, 'some string etc.'
FROM    table2
WHERE   table2.ID = 7;

我只使用过Access、SQL 2000/2005/Express、MySQL 和 PostgreSQL 都使用此语法,因此应该涵盖这些语法。 它也应该与 SQLite3 一起使用。

Claude Houle's answer: should work fine, and you can also have multiple columns and other data as well:

INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT  table2.column1, table2.column2, 8, 'some string etc.'
FROM    table2
WHERE   table2.ID = 7;

I've only used this syntax with Access, SQL 2000/2005/Express, MySQL, and PostgreSQL, so those should be covered. It should also work with SQLite3.

柠檬 2024-07-11 12:32:10

只需使用 SELECT 查询,而不是 INSERT 查询的 VALUES 部分,如下所示。

INSERT INTO table1 ( column1 , 2, 3... )
SELECT col1, 2, 3... FROM table2

Instead of VALUES part of INSERT query, just use SELECT query as below.

INSERT INTO table1 ( column1 , 2, 3... )
SELECT col1, 2, 3... FROM table2
旧城空念 2024-07-11 12:32:10

要在第一个答案中添加一些内容,当我们只需要另一个表中的几条记录(在本例中只有一条)时:

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3, COLUMN4) 
VALUES (value1, value2, 
(SELECT COLUMN_TABLE2 
FROM TABLE2
WHERE COLUMN_TABLE2 like "blabla"),
value4);

To add something in the first answer, when we want only few records from another table (in this example only one):

INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3, COLUMN4) 
VALUES (value1, value2, 
(SELECT COLUMN_TABLE2 
FROM TABLE2
WHERE COLUMN_TABLE2 like "blabla"),
value4);
作死小能手 2024-07-11 12:32:10

使用 select 子查询插入的两种方法。

  1. 使用 SELECT 子查询返回一行结果。
  2. 使用 SELECT 子查询返回多行结果。

1. 使用 SELECT 子查询返回一行结果的方法。

INSERT INTO <table_name> (<field1>, <field2>, <field3>) 
VALUES ('DUMMY1', (SELECT <field> FROM <table_name> ),'DUMMY2');

在这种情况下,它假设 SELECT 子查询基于 WHERE 条件或 SUM、MAX 等 SQL 聚合函数仅返回一行结果,AVG等。否则会抛出错误

2。 使用 SELECT 子查询返回多行结果的方法。

INSERT INTO <table_name> (<field1>, <field2>, <field3>) 
SELECT 'DUMMY1', <field>, 'DUMMY2' FROM <table_name>;

第二种方法适用于这两种情况。

Two approaches for insert into with select sub-query.

  1. With SELECT subquery returning results with One row.
  2. With SELECT subquery returning results with Multiple rows.

1. Approach for With SELECT subquery returning results with one row.

INSERT INTO <table_name> (<field1>, <field2>, <field3>) 
VALUES ('DUMMY1', (SELECT <field> FROM <table_name> ),'DUMMY2');

In this case, it assumes SELECT Sub-query returns only one row of result based on WHERE condition or SQL aggregate functions like SUM, MAX, AVG etc. Otherwise it will throw error

2. Approach for With SELECT subquery returning results with multiple rows.

INSERT INTO <table_name> (<field1>, <field2>, <field3>) 
SELECT 'DUMMY1', <field>, 'DUMMY2' FROM <table_name>;

The second approach will work for both the cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文