
- 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 do I INSERT INTO from one MySQL table into another table and set the value of one column?
Let us first create a table. Following is the query −
mysql> create table insertOneToAnotherTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.60 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into insertOneToAnotherTable values(100); Query OK, 1 row affected (0.08 sec) mysql> insert into insertOneToAnotherTable values(200); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable values(300); Query OK, 1 row affected (0.13 sec) mysql> insert into insertOneToAnotherTable values(400); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable values(500); Query OK, 1 row affected (0.12 sec) mysql> insert into insertOneToAnotherTable values(600); Query OK, 1 row affected (0.16 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from insertOneToAnotherTable;
This will produce the following output −
+-------+ | Value | +-------+ | 100 | | 200 | | 300 | | 400 | | 500 | | 600 | +-------+ 6 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table recieveDateFromTable -> ( -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.83 sec)
Following is the query to INSERT INTO from one MySQL table into another table and set the value of one column −
mysql> insert into recieveDateFromTable(Value1,Value2) select Value,1000 from insertOneToAnotherTable; Query OK, 6 rows affected (0.14 sec) Records: 6 Duplicates: 0 Warnings: 0
Let us display all records from the second table. Following is the query −
mysql> select * from recieveDateFromTable;
This will produce the following output −
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 100 | 1000 | | 200 | 1000 | | 300 | 1000 | | 400 | 1000 | | 500 | 1000 | | 600 | 1000 | +--------+--------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- How to insert values from one table into another in PostgreSQL?
- Copy column values from one table into another matching IDs in MySQL
- How do I insert a record from one Mongo database into another?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- How do I insert all elements from one list into another in Java?
- Insert data from one table to another in MySQL?
- MySQL query for INSERT INTO using values from another table?
- MySQL trigger to insert row into another table?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- 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?
- Insert from one table with different structure to another in MySQL?
- Select some data from a database table and insert into another table in the same database with MySQL
- Insert JSON into a MySQL table?

Advertisements