
- 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
Select into in MySQL?
To do select into in MySQL, use CREATE TABLE SELECT command. The syntax is as follows −
CREATE TABLE yourTableName SELECT *FROM yourOriginalTableName;
To understand, let us first create a table −
mysql> create table SelectIntoDemo -> ( -> Id int, -> Name varchar(200) -> ); Query OK, 0 rows affected (0.50 sec)
Let us insert some records into the table with the help of insert command. The query is as follows −
mysql> insert into SelectIntoDemo values(1,'Bob'),(2,'Carol'),(3,'David'); Query OK, 3 rows affected (0.15 sec) Records: 3 Duplicates: 0 Warnings: 0
Displaying all records with the help of select statement. The query is as follows −
mysql> select *from SelectIntoDemo;
Here is the output −
+------+-------+ | Id | Name | +------+-------+ | 1 | Bob | | 2 | Carol | | 3 | David | +------+-------+ 3 rows in set (0.00 sec)
Now you can apply the above syntax discussed in the beginning. The query is as follows −
mysql> create table yourTempTable select *from SelectIntoDemo; Query OK, 3 rows affected (0.56 sec) Records: 3 Duplicates: 0 Warnings: 0
Check all records with new table name “yourTempTable”. The query is as follows −
mysql> select *from yourTempTable;
The following is the output −
+------+-------+ | Id | Name | +------+-------+ | 1 | Bob | | 2 | Carol | | 3 | David | +------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- What is the MySQL SELECT INTO Equivalent?
- Perform MySQL SELECT INTO user-defined variable
- Select into a user-defined variable with MySQL
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- MySQL select distinct rows into a comma delimited list column?
- How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?
- How to add static value while INSERT INTO with SELECT in a MySQL query?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- MySQL Select IN range?
- Perform MySQL SELECT on dates inserted into the table as VARCHAR values
- SELECT increment counter in MySQL?
- Select highest salary in MySQL?
- MYSQL - select database?
- SELECT WHERE IN null in MySQL?

Advertisements