sql选择性插入_SQL插入选择
sql選擇性插入
In the real world of SQL, imagine if there is a situation when you need to copy data from one table to another table. SQL has an answer for this and the answer is “SQL Insert Into Select“.
在SQL的真實世界中,想象一下是否存在需要將數(shù)據(jù)從一個表復(fù)制到另一表的情況。 SQL對此有一個答案,答案是“ SQL Insert In Select ”。
SQL插入選擇 (SQL Insert Into Select)
SQL Insert Into Select clause is used when we want to copy data from one table to another table.
當我們要將數(shù)據(jù)從一個表復(fù)制到另一表時,將使用SQL Insert Into Select子句。
SQL插入選擇規(guī)則 (Rules For SQL Insert Into Select)
- INSERT INTO SELECT requires that data types in source and target tables match. INSERT INTO SELECT要求源表和目標表中的數(shù)據(jù)類型匹配。
- The existing records in the target table are unaffected. 目標表中的現(xiàn)有記錄不受影響。
SQL插入選擇語法 (SQL Insert Into Select Syntax)
INSERT INTO table2 (column1, column2, ... , columnN) SELECT c1, c2, ... , cn FROM table1 WHERE condition;In the above syntax, data from table1 is selected using SELECT statement and then is inserted into the table2 using?INSERT statement.
在以上語法中,使用SELECT語句選擇來自table1的數(shù)據(jù),然后使用INSERT語句將其插入到table2中。
SQL插入選擇示例 (SQL Insert Into Select Example)
Let us consider the following table for understanding SQL Insert Into Select Statement.
讓我們考慮下表,以了解將SQL插入到Select語句中。
Teacher
老師
| 1 | Amit | Bengaluru | India |
| 2 | Harry | Texas | US |
| 3 | John | London | UK |
| 1個 | 阿米特 | 班加羅爾 | 印度 |
| 2 | 哈里 | 德州 | 我們 |
| 3 | 約翰 | 倫敦 | 英國 |
Student
學(xué)生
| 1 | Henry | Wales | UK |
| 2 | Rohit | Delhi | India |
| 3 | Steve | London | UK |
| 1個 | 亨利 | 威爾士 | 英國 |
| 2 | 羅希特 | 新德里 | 印度 |
| 3 | 史蒂夫 | 倫敦 | 英國 |
Query for the tables:
查詢表 :
CREATE TABLE `teacher` (`TeacherId` INT NOT NULL,`TeacherName` VARCHAR(45) NULL,`State` VARCHAR(45) NULL,`Country` VARCHAR(45) NULL,PRIMARY KEY (`TeacherId`),UNIQUE INDEX `TeacherId_UNIQUE` (`TeacherId` ASC) VISIBLE);CREATE TABLE `student` (`StudentId` INT NOT NULL,`StudentName` VARCHAR(45) NULL,`State` VARCHAR(45) NULL,`Country` VARCHAR(45) NULL,PRIMARY KEY (`StudentId`),UNIQUE INDEX `StudentId_UNIQUE` (`StudentId` ASC) VISIBLE);Insert into Teacher(TeacherId,TeacherName,State,Country) VALUES (1, 'Amit','Bengaluru','India'), (2, 'Harry','Texas','US'), (3, 'John','London','UK'); Insert into Student(StudentId,StudentName,State,Country) VALUES (1, 'Henry','Wales','UK'), (2, 'Rohit','Delhi','India'), (3, 'Steve','London','UK');Let us assume a case when the Student from India got a teaching?job in the same Institute. In that case, the data for students from India need to be copied to the data in Teacher table.
讓我們假設(shè)一個案例,即印度學(xué)生在同一所學(xué)院獲得教學(xué)工作。 在這種情況下,需要將印度學(xué)生的數(shù)據(jù)復(fù)制到“教師”表中的數(shù)據(jù)。
Insert into Teacher (TeacherId,TeacherName,State,Country) Select 4,StudentName,State,Country from Student where country = 'India';Notice that there is already a teacher with id 2, so we are using “select 4” to use a different id for the student data that we are copying to the teacher table.
請注意,已經(jīng)有一個ID為2的教師,因此我們使用“選擇4”為復(fù)制到教師表的學(xué)生數(shù)據(jù)使用不同的ID。
Below image shows the teacher table data after the command execution.
下圖顯示了命令執(zhí)行后的教師表格數(shù)據(jù)。
SQL Insert Into Select
SQL插入選擇
結(jié)論 (Conclusion)
SQL insert into select clause is very helpful in copying data from one table to another. We can use it to create a selective dump of a table data. It’s supported by all the major SQL database vendors such as MySQL, Oracle, SQL Server, PostgreSQL etc.
將SQL插入select子句對于將數(shù)據(jù)從一個表復(fù)制到另一個表非常有幫助。 我們可以使用它來創(chuàng)建表數(shù)據(jù)的選擇性轉(zhuǎn)儲。 所有主要SQL數(shù)據(jù)庫供應(yīng)商(例如MySQL,Oracle,SQL Server,PostgreSQL等)都支持它。
翻譯自: https://www.journaldev.com/24344/sql-insert-into-select
sql選擇性插入
總結(jié)
以上是生活随笔為你收集整理的sql选择性插入_SQL插入选择的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5毛钱搞一个2.4GHz射频信号探测器
- 下一篇: 聊聊身边的嵌入式,英语学习利器点读笔