
- 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 with a Select query in MySQL
For Insert with SELECT query, the syntax is as follows −
insert into yourTableName(yourColumnName1,yourColumnName2,yourColumnName3,...N) select yourValue1,yourValue2,yourValue3,......N;
Let us first create a table −
mysql> create table DemoTable1603 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 101,'John',45; Query OK, 1 row affected (0.17 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 102,'Adam',76; Query OK, 1 row affected (0.11 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 103,'Bob',67; Query OK, 1 row affected (0.31 sec) Records: 1 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1603;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 101 | John | 45 | | 102 | Adam | 76 | | 103 | Bob | 67 | +-----------+-------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL select and insert in two tables with a single query
- How to add static value while INSERT INTO with SELECT in a MySQL query?
- Select query with regular expression in MySQL
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- MySQL select query with multiple WHERE?
- MySQL query to insert row with date?
- Can we implement nested insert with select in MySQL?
- Select and insert values with preceding zeros in a MySQL table
- A single MySQL query to select value from first table and insert in the second?
- Select the table name as a column in a UNION select query with MySQL?
- Insert multiple values in a temporary table with a single MySQL query?
- MySQL query to select records with a particular date?
- MySQL SELECT from two tables with a single query
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Fastest way to insert with multiple values in a single MySQL query?

Advertisements