
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Insert data from one table to another in MySQL?
To insert data from one table to another, use the INSERT INTO SELECT statement. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(101,'Adam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(102,'John'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 101 | Adam | | 102 | John | +------+-----------+ 2 rows in set (0.00 sec)
Following is the query to create the second table −
mysql> create table DemoTable2 -> ( -> EmployeeId int, -> EmployeeName varchar(20) -> ); Query OK, 0 rows affected (0.42 sec)
Here is the query to insert data from one table to another in MySQL −
mysql> insert into DemoTable2(EmployeeId,EmployeeName) select Id,FirstName from DemoTable1; Query OK, 2 rows affected (0.15 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 −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 101 | Adam | | 102 | John | +------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- Insert data from one schema to another in MySQL?
- Insert from one table with different structure to another in MySQL?
- Update data in one table from data in another table in MySQL?
- MySQL query to insert data from another table merged with constants?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Take all records from one MySQL table and insert it to another?
- Simplest way to copy data from one table to another new table in MySQL?
- How to insert values from one table into another in PostgreSQL?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Move rows from one table to another in MySQL?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Select some data from a database table and insert into another table in the same database with MySQL
- MySQL query for INSERT INTO using values from another table?
- MySQL trigger to insert row into another table?

Advertisements