Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(100) -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1(FirstName) values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1(FirstName) values('Chris');
Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Chris | +----+-----------+ 2 rows in set (0.00 sec)
Now create the second table −
mysql> create table DemoTable2 -> ( -> CustomerId int, -> CustomerName varchar(100) -> ); Query OK, 0 rows affected (0.43 sec)
Now you can insert some records in the above table using INSERT INTO SELECT command −
mysql> insert into DemoTable2(CustomerId,CustomerName) -> select Id,FirstName from DemoTable1; Query OK, 2 rows affected (0.17 sec) Records: 2 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 1 | John | | 2 | Chris | +------------+--------------+ 2 rows in set (0.00 sec)
Advertisements
