Get the App
SLTechnology News&Howtos  ›  Development  › 

How does SQL insert data from one table into another

Shulou Source: shulou.com Published: 2022-06-01 13:05:55 09月18日 Update

This article mainly introduces SQL how to insert data in a table into another table of related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this SQL how to insert data in a table into another table the article will have a harvest, let's take a look at it.

Create a test table MyStudentInfoCREATE table MyStudentInfo (Id int not null primary key, Name varchar (16), Age int, Gender varchar (2), Phone varchar (16), Address varchar (50), GradeId int) to insert multiple pieces of data, INSERT INTO MyStudentInfoSELECT 1, Zhang San, 20, and 1 UNIONSELECT 2, Li Si, 22, 12345678901, Wang Wu, 13976891234, 13976891234, respectively. 'Tianjin', 2 UNIONSELECT 4, Zhao Liu, 19 UNIONSELECT, 18676891234, 18676891234, 3 UNIONSELECT 5, Xiaohong, 21, 17776891234, 4 UNIONSELECT 6, Wang, 25, 13176891234, 2 UNIONSELECT, 7 Liu, 13374591234, 13374591234, Nanjing, 1, one, one, and one sentence selects data from a table. Then insert the data into another table

1. Insert all the field data of the MyStudentInfo table into a table that does not exist

SELECT * INTO studentinfo_test1 FROM MyStudentInfo

2. Insert individual field data of the MyStudentInfo table into a table that does not exist.

SELECT Id,Name INTO MytestInfo FROM myStudentInfo

3. With where clause

SELECT Id,Name,Gender INTO MytestInfo FROM myStudentInfo WHERE Gender='1'

Query the data of MytestInfo table

4. Select data from more than one table to insert into the new table

SELECT s.Id,s.Name,s.GradeId,g.GradeName INTO NewTable FROM MyStudentInfo s INNER JOIN GradeInfo g on s.GradeId=g.Id

Query the data of NewTable table

Insert the data in the studentinfo table into the existing table

Insert the id,name column of the MyStudentInfo table into the studentinfo_test2 (studentinfo_ Test2 table already exists)

INSERT INTO studentinfo_test2SELECT * FROM MyStudentInfo's article on "how SQL inserts data from one table into another" ends here. Thank you for reading! I believe that everyone has a certain understanding of "how SQL inserts data from one table into another". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Data knowledge content fields articles queries values multiple clauses easy to operate easy to understand more look industry table sentence information information channel face channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Apple OPPO Reno macOS