日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

sql选择性插入_SQL插入选择

發布時間:2023/12/20 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的真實世界中,想象一下是否存在需要將數據從一個表復制到另一表的情況。 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.

當我們要將數據從一個表復制到另一表時,將使用SQL Insert Into Select子句。

SQL插入選擇規則 (Rules For SQL Insert Into Select)

  • INSERT INTO SELECT requires that data types in source and target tables match.

    INSERT INTO SELECT要求源表和目標表中的數據類型匹配。
  • The existing records in the target table are unaffected.

    目標表中的現有記錄不受影響。

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的數據,然后使用INSERT語句將其插入到table2中。

SQL插入選擇示例 (SQL Insert Into Select Example)

Let us consider the following table for understanding SQL Insert Into Select Statement.

讓我們考慮下表,以了解將SQL插入到Select語句中。

Teacher

老師

TeacherIdTeacherNameStateCountry
1AmitBengaluruIndia
2HarryTexasUS
3JohnLondonUK
教師編號 老師的名字 州 國家
1個 阿米特 班加羅爾 印度
2 哈里 德州 我們
3 約翰 倫敦 英國

Student

學生

StudentIdStudentNameStateCountry
1HenryWalesUK
2RohitDelhiIndia
3SteveLondonUK
學生卡 學生姓名 州 國家
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.

讓我們假設一個案例,即印度學生在同一所學院獲得教學工作。 在這種情況下,需要將印度學生的數據復制到“教師”表中的數據。

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.

請注意,已經有一個ID為2的教師,因此我們使用“選擇4”為復制到教師表的學生數據使用不同的ID。

Below image shows the teacher table data after the command execution.

下圖顯示了命令執行后的教師表格數據。

SQL Insert Into Select

SQL插入選擇

結論 (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子句對于將數據從一個表復制到另一個表非常有幫助。 我們可以使用它來創建表數據的選擇性轉儲。 所有主要SQL數據庫供應商(例如MySQL,Oracle,SQL Server,PostgreSQL等)都支持它。

翻譯自: https://www.journaldev.com/24344/sql-insert-into-select

sql選擇性插入

總結

以上是生活随笔為你收集整理的sql选择性插入_SQL插入选择的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。