- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert the results of a MySQL select? Is it possible?
152 Lectures 16 hours
87 Lectures 5.5 hours
You do not need to use values whenever you insert the results of a select. To insert the results of select, let us first create two tables.
The first table query is as follows −
< FirstTableDemo> mysql> create table FirstTableDemo −> ( −> StudentId int, −> StudentName varchar(100) −> ); Query OK, 0 rows affected (0.41 sec)
Now create second table and after that insert the second table records in the first table using INSERT SELECT command.
The query to create the second table −
<SecondTableDemo> mysql> create table SecondTableDemo −> ( −> Id int, −> Name varchar(100) −> ); Query OK, 0 rows affected (0.47 sec)
Insert records in the table using insert command. The query is as follows −
mysql> insert into SecondTableDemo values(1,'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into SecondTableDemo values(2,'Sam'); Query OK, 1 row affected (0.27 sec)
Now we have two records in the second table. The query to display all records from table using select statement is as follows −
mysql> select *from SecondTableDemo;
The following is the output −
+------+------+ | Id | Name | +------+------+ | 1 | John | | 2 | Sam | +------+------+ 2 rows in set (0.00 sec)
Insert all records of second table into first table using INSERT SELECT statement −
mysql> insert into FirstTableDemo(StudentId,StudentName) −> select Id,Name from SecondTableDemo as tbl1 −> where tbl1.Id not in (select StudentId from FirstTableDemo); Query OK, 2 rows affected (0.57 sec) Records: 2 Duplicates: 0 Warnings: 0
Now we have inserted all records of second table to the first table. Let us cross check using select statement. The query is as follows −
mysql> select *from FirstTableDemo;
The following is the output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | John | | 2 | Sam | +-----------+-------------+ 2 rows in set (0.00 sec)
- Can we select row by DATEPART() in MySQL? Is it possible?
- Select results from the middle of a sorted list in MySQL?
- Is it possible to make an insert or an update in the same MySQL query?
- Is it possible to exclude subclasses from the results displayed in backoffice in SAP?
- Limit length of longtext field in MySQL SELECT results?
- MySQL SELECT to skip first N results?
- A single MySQL select query on two tables is possible?
- Insert with a Select query in MySQL
- Insert record using MySQL SELECT?
- Is it possible to calculate a correlation in a MySQL query?
- Combine SELECT & SHOW command results in MySQL?
- Is it possible to have a function-based index in MySQL?
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
- Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?