2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 了解SQL INSERT INTO SELECT语句

了解SQL INSERT INTO SELECT语句

时间:2022-12-23 15:43:04

相关推荐

了解SQL INSERT INTO SELECT语句

In this article, we will be focusing on Working ofSQL INSERT INTO SELECTstatement 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;ORSelect 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查询以执行以下操作–

select the data values of the column ‘Price’ and ‘City’ from ‘Info’从“信息”中选择“价格”和“城市”列的数据值 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 SELECTINSERT INTO SELECTexample 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 WholesaleSELECT * 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

翻译自: /41503/sql-insert-into-select-statement

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。