
- 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
MySQL query to insert data from another table merged with constants?
Let us first create a table −
mysql> create table DemoTable1(Name varchar(100)); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values('Robert'); 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 −
+--------+ | Name | +--------+ | John | | Chris | | Robert | +--------+ 3 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2( ConstantValue int, EmployeeName varchar(100) ); Query OK, 0 rows affected (0.70 sec)
Following is the query to insert data from another table merged with constants −
mysql> insert into DemoTable2(ConstantValue,EmployeeName) select '1000',Name from DemoTable1; Query OK, 3 rows affected (0.13 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+---------------+--------------+ | ConstantValue | EmployeeName | +---------------+--------------+ | 1000 | John | | 1000 | Chris | | 1000 | Robert | +---------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- Insert data from one table to another in MySQL?
- MySQL query for INSERT INTO using values from another table?
- MySQL statement to copy data from one table and insert into another table
- 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 data from one schema to another in MySQL?
- MySQL query to copy records from one table to another with different columns
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL trigger to insert row into another table?
- Take all records from one MySQL table and insert it to another?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Update data in one table from data in another table in MySQL?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Updating a MySQL table with values from another table?
- Simplest way to copy data from one table to another new table in MySQL?

Advertisements