
- 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
How to SELECT fields from one table and INSERT to another in MySQL?
Let us first create a table −
mysql> create table DemoTable1 -> ( -> StudentId int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(10,'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(11,'Chris'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 10 | John | | 11 | Chris | +-----------+-------------+ 2 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 -> ( -> CustomerId int, -> CustomerName varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)
Here is the query to SELECT fields from one table and INSERT to another −
mysql> insert into DemoTable2(CustomerId,CustomerName) select StudentId,StudentName from DemoTable1 where StudentId=11; Query OK, 1 row affected (0.20 sec) Records: 1 Duplicates: 0 Warnings: 0
Let us check all the records from the second table again −
mysql> select *from DemoTable2;
Output
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 11 | Chris | +------------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- Insert data from one table to another in MySQL?
- MySQL statement to copy data from one table and insert into another table
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Insert from one table with different structure to another in MySQL?
- Take all records from one MySQL table and insert it to another?
- How to insert values from one table into another in PostgreSQL?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Insert data from one schema to another in MySQL?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- How to copy rows from one table to another in MySQL?
- Select some data from a database table and insert into another table in the same database with MySQL
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- MySQL select query to select rows from a table that are not in another table?
- Move rows from one table to another in MySQL?
- How to copy a table from one MySQL database to another?

Advertisements