
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Insert the results of a MySQL select? Is it possible?
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)
- Related Articles
- 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?
- Insert with a Select query in MySQL
- Insert record using MySQL SELECT?
- Combine SELECT & SHOW command results in MySQL?
- Limit length of longtext field in MySQL SELECT results?
- A single MySQL select query on two tables is possible?
- Is it possible to select text boxes with JavaScript?
- MySQL SELECT to skip first N results?\n
- Is it possible to exclude subclasses from the results displayed in backoffice in SAP?
- How to order results of a query randomly & select random rows in MySQL?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- Select * but ignore displaying results containing a specific character in MySQL
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
