In this article, we will be focusing on Working of SQL INSERT INTO SELECT statement altogether.

在本文中,我们将全神贯注于SQL INSERT INTO SELECT语句的工作。



什么是SQL SELECT语句? (What is SQL SELECT statement?)

SQL SELECT query displays the specified data from the table of a database.

SQL SELECT查询显示数据库表中的指定数据。

Syntax:

句法:


Select * from Table;OR
Select column-list from Table;

We can either display all the data or a portion of it engrossed by conditions.

我们可以显示所有数据,也可以显示条件所占据的一部分数据。

Example:

例:


Select * from Info;


什么是SQL INSERT语句? (What is SQL INSERT statement?)

SQL INSERT query is useful in inserting records into the database according to the specified columns and conditions.

SQL INSERT查询对于根据指定的列和条件将记录插入数据库中很有用。

Syntax:

句法:


Insert into Table(column-list)values(val1,,,,valN);


SQL INSERT INTO SELECT语句的工作 (Working of SQL INSERT INTO SELECT statement)

SQL INSERT INTO SELECT statement enables us to select and copy data from one table to another.

SQL INSERT INTO SELECT语句使我们能够选择数据并将其从一个表复制到另一个表。

Syntax:

句法:


INSERT INTO Table2 (Column-list)
SELECT(Column-list) From Table1;

This statement executes in the following manner:

该语句以以下方式执行:

  • Initially, it selects data from the specified columns of the table1.最初,它从table1的指定列中选择数据。
  • Further, the selected data from the columns of table1 is inserted into the specified columns of table2此外,将从表1的列中选择的数据插入到表2的指定列中

Thus, by using INSERT INTO SELECT, we can copy some particular data from a table and insert it into another table.

因此,通过使用INSERT INTO SELECT,我们可以从表中复制某些特定数据并将其插入到另一个表中。

The existing data values of table1 and table2 does not get affected by the INSERT INTO SELECT query.

table1和table2的现有数据值不受INSERT INTO SELECT查询的影响。

Having understood the working of the INSERT INTO SELECT query, let us now understand the syntax of the same in the below section.

在了解了INSERT INTO SELECT查询的工作原理之后,现在让我们在下面的部分中了解其语法。



通过示例实现INSERT INTO SELECT (Implementing INSERT INTO SELECT through examples)

Initially, we create a Table 1 — ‘Info’ using SQL Create query with the following columns:

最初,我们使用以下几列使用SQL Create查询创建表1 —“信息”:

  • item_iditem_id
  • Price价钱
  • Quantity数量
  • City市

create table Info(item_id integer, Price integer, Quantity integer, City varchar(200);

Then, we insert records into the table 1 — ‘Info’ using SQL Insert query.

然后,使用SQL插入查询将记录插入表1 —“信息”中。


insert into Info(item_id, Price,Quantity,City) values(1, 150,30,'Pune');
insert into Info(item_id, Price,Quantity,City) values(2, 100,23,'USA');
insert into Info(item_id, Price,Quantity,City) values(3, 250,5,'UAE');
insert into Info(item_id, Price,Quantity,City) values(3, 800,2,'DENMARK');

Select * FROM Info;

Output: Table 1 — ‘Info’

输出: 表1 —“信息”

INSERT INTO SELECT Table 1插入选择表1

Further, we create Table 2 – ‘Wholesale’ with the following data columns:

此外,我们使用以下数据列创建表2 –“批发”:

  • Object_idObject_id
  • Price价钱
  • Quantity数量
  • City市

create table Wholesale(Object_id integer, Price integer, Quantity integer, City varchar(200);

The data values into ‘Wholesale’ are inserted using INSERT query of SQL.

使用SQL的INSERT查询将“ Wholesale”中的数据值插入。


insert into Wholesale(Object_id, Price,Quantity,City) values(1, 2,1500,'DENMARK');
insert into Wholesale(Object_id, Price,Quantity,City) values(2, 4,2000,'USA');
insert into Wholesale(Object_id, Price,Quantity,City) values(3, 250,157,'USA');
insert into Wholesale(Object_id, Price,Quantity,City) values(4, 60,645,'DENMARK');
insert into Wholesale(Object_id, Price,Quantity,City) values(5, 25,45,'Pune');

SELECT * FROM Wholesale;

Output: Table 2 — ‘Wholesale’

输出: 表2 —“批发”

INSERT INTO SELECT Table 2插入选择表2

Now, In this example, we execute the SQL INSERT INTO SELECT query to perform the following–

现在,在此示例中,我们执行SQL INSERT INTO SELECT查询以执行以下操作–

  1. select the data values of the column ‘Price’ and ‘City’ from ‘Info’从“信息”中选择“价格”和“城市”列的数据值
  2. insert and copy the above-selected records under the column ‘Price’ and ‘City’ of the ‘Wholesale’ table.在“批发”表的“价格”和“城市”列下插入并复制上面选择的记录。

INSERT INTO WholeSale (Price, City)
SELECT Price, City FROM Info;

Output:

输出:

INSERT INTO SELECT INSERT INTO SELECT example 1示例1

In this example, we have selected and copied all the data values from the table ‘Info’ and inserted it into the table ‘Wholesale’.

在此示例中,我们从表“信息”中选择并复制了所有数据值,并将其插入到表“批发”中。


INSERT INTO Wholesale
SELECT * FROM Info;

Note: The data values are copied from table 1 to table 2. But the original data values of the table(from which the data is being copied) remain unaltered.

注意:数据值从表1复制到表2。但是表(从中复制数据)的原始数据值保持不变。

Output:

输出:

SQL INSERT INTO SELECT Example 2SQL INSERT INTO SELECT示例2


结论 (Conclusion)

By this, we have come to the end of this topic. The INSERT INTO SELECT query can be further used alongside different clauses such as WHERE clause, GROUP BY clause, etc. I recommend you to try these out.

至此,我们到了本主题的结尾。 INSERT INTO SELECT查询可以与WHERE子句 ,GROUP BY子句等不同子句一起使用。我建议您尝试一下。

For more posts related to SQL, please do visit SQL JournalDev.

有关SQL的更多信息,请访问SQL JournalDev 。

And feel free to comment below in case, you come across any doubt!

如果您遇到任何疑问,请在下面发表评论。



参考资料 (References)

  • SQL INSERT INTO SELECT — StackOverFlowSQL插入选择— StackOverFlow

翻译自: https://www.journaldev.com/41503/sql-insert-into-select-statement

了解SQL INSERT INTO SELECT语句相关推荐

  1. SQL INSERT INTO SELECT 语句

    SQL INSERT INTO SELECT 语句 通过SQL,可以从一个表复制信息到另一个表. INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中.目标表中 ...

  2. SQL语句--INSERT INTO SELECT 语句用法示例

    通过 SQL,您可以从一个表复制信息到另一个表. INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中. SQL INSERT INTO SELECT 语句 I ...

  3. SQL学习之insert into select语句

    目录 参考源 SQL insert into select 语句 示例数据 SQL insert into select 使用 参考源 简单教程 https://www.twle.cn/l/yufei ...

  4. INSERT INTO SELECT语句概述和示例

    This article covers the SQL INSERT INTO SELECT statement along with its syntax, examples and use cas ...

  5. SQL Server 中 SELECT INTO 和 INSERT INTO SELECT语句的区别

    SQL Server 中 SELECT INTO 和 INSERT INTO SELECT语句的区别 我们在写存储过程的时候经常会遇到需要将查询到的数据存到一张表里面的情况,如将一个table1的数据 ...

  6. SQL的INSERT INTO和INSERT INTO SELECT语句

    INSERT INTO语句用来给一个table插入信息的records. 语法: 第一种,指定列名和插入的值 INSERT INTO table_name (column1, column2, col ...

  7. 因用了Insert into select语句,同事被开除了!

    " Insert into select 请慎用,同事因为使用了 Insert into select 语句引发了重大生产事故,最后被开除. 作者:不一样的科技宅 https://jueji ...

  8. insert into select语句锁表故障

    深入研究insert into select语句锁表故障(上) 故障描述 前几天,一个mysql数据库运维同事,在生产上用insert into select * from语句,在生产上备份了一张表, ...

  9. INSERT INTO SELECT语句与SELECT INTO FROM语句区别

    1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,-) select value1,value2,- from Table1 ...

最新文章

  1. 10分钟带你深入理解Transformer原理及实现
  2. Kali Linux Wine32英文字体不显示问题
  3. 计算机设备及网络建设使用情况,高校信息化网络基础设施建设状况对比
  4. Python进度条,可用在for循环中查看循环个数的执行情况(很实用)
  5. SAP UI5 bindProperty的实现
  6. 看嵌入式大神直播,送开发板!
  7. Cloud Prizefight: OpenStack vs. VMware(转)-HA-FT
  8. 2018可能大火的物联网应用
  9. 2018北京ICPC D. Frog and Portal(构造)
  10. ImportError: libcudart.so.9.2: cannot open shared object file: No such file or directory
  11. 博途PLC和ABB变频器PN通讯详解
  12. 啦啦外卖43.5学习研究开发
  13. 2D激光雷达运动畸变矫正_base里程计
  14. Data too long for column ‘password‘ at row 1“
  15. idea常用的一些配置信息
  16. python tk checkbutton_Python tkinter之CheckButton(多选框)
  17. 牛客网错题集合之字符串(一)
  18. python综合练习:学生管理系统
  19. 忆往昔,看今朝,望未来
  20. 数字孪生电梯模型构建方案

热门文章

  1. NRF24L01 + STC15F204EA 无线通信 源代码
  2. volatile 变量
  3. 在老ASP中使用对象的对象生存期问题
  4. [转载] python的面向对象和类与对象
  5. [转载] python numpy np.exp()函数
  6. 39. 组合总和 ,40. 组合总和 II,216. 组合总和 III
  7. 25个深度学习开源数据集
  8. js中style.display=无效的解决方法
  9. yolov3前向传播(三)-- 坐标转换,iou计算,权重加载,图片显示
  10. SLAM的一些基础知识