插入...值(SELECT ... FROM ...) [英] Insert into ... values ( SELECT ... FROM ... )
问题描述
我试图使用来自另一个表的输入插入到表中。虽然这对于许多数据库引擎是完全可行的,但我总是很难记住当天的SQL引擎的正确语法( MySQL , Oracle , SQL Server , Informix 和 DB2 )。
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).
有一个来自SQL标准的silver-bullet语法(例如SQL-92 ),这将允许我插入值而不用担心底层数据库?
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?
推荐答案
尝试:
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
这是标准的ANSI SQL,应该可以在任何DBMS
This is standard ANSI SQL and should work on any DBMS
它绝对适用于:
- Oracle
- MS SQL Server
- MySQL
- Postgres
- SQLite v3
- Teradata
- DB2
- Vertica
- H2
- AWS RedShift
- Oracle
- MS SQL Server
- MySQL
- Postgres
- SQLite v3
- Teradata
- DB2
- Sybase
- Vertica
- HSQLDB
- H2
- AWS RedShift
这篇关于插入...值(SELECT ... FROM ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!