Home > Database > Mysql Tutorial > Insert the results of MySQL select? Is it possible?

Insert the results of MySQL select? Is it possible?

王林
Release: 2023-09-11 16:29:07
forward
1100 people have browsed it

插入 MySQL select 的结果?是否可以?

No need to use values ​​whenever inserting selection results. In order to insert the results of select, we first create two tables.

The first table query is as follows -

< FirstTableDemo>
mysql> create table FirstTableDemo
   &minus;> (
   &minus;> StudentId int,
   &minus;> StudentName varchar(100)
   &minus;> );
Query OK, 0 rows affected (0.41 sec)
Copy after login

Now create the second table, and then use the INSERT SELECT command to insert the records of the second table into the first table.

The query to create the second table is as follows:

<SecondTableDemo>

mysql> create table SecondTableDemo
   &minus;> (
   &minus;> Id int,
   &minus;> Name varchar(100)
   &minus;> );
Query OK, 0 rows affected (0.47 sec)
Copy after login

Use the insert command to insert records in the table. The query is as follows −

mysql> insert into SecondTableDemo values(1,&#39;John&#39;);
Query OK, 1 row affected (0.14 sec)

mysql> insert into SecondTableDemo values(2,&#39;Sam&#39;);
Query OK, 1 row affected (0.27 sec)
Copy after login

Now there are two records in the second table. The query that uses the select statement to display all records in the table is as follows -

mysql> select *from SecondTableDemo;
Copy after login

The following is the output−

+------+------+
| Id   | Name |
+------+------+
|    1 | John |
|    2 | Sam  |
+------+------+
2 rows in set (0.00 sec)
Copy after login

Use the INSERT SELECT statement to insert all the records of the second table into the first table−

mysql> insert into FirstTableDemo(StudentId,StudentName)
   &minus;> select Id,Name from SecondTableDemo as tbl1
   &minus;> where tbl1.Id not in (select StudentId from FirstTableDemo);
Query OK, 2 rows affected (0.57 sec)
Records: 2 Duplicates: 0 Warnings: 0
Copy after login

Now we have inserted all the records from the second table into the first table. Let's cross check using select statement. The query is as follows −

mysql> select *from FirstTableDemo;
Copy after login

The following is the output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | John        |
|         2 | Sam         |
+-----------+-------------+
2 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Insert the results of MySQL select? Is it possible?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template