
- 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 select and insert in two tables with a single query
Here is the query to create first table.
mysql> create table DemoTable1 -> ( -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.67 sec)
To understand the above concept, let us create second table.
mysql> create table DemoTable2 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values('Chris'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | +-------+ 1 row in set (0.00 sec)
Here is the query to select and insert records with a single MySQL query −
mysql> insert into DemoTable1 -> select Name,89 from DemoTable2 -> union all -> select Name,98 from DemoTable2; Query OK, 2 rows affected (0.15 sec) Records: 2 Duplicates: 0 Warnings: 0
Now you can select records from the first −
mysql> select * from DemoTable1;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 89 | | Chris | 98 | +-------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL SELECT from two tables with a single query
- A single MySQL select query on two tables is possible?
- How to insert into two tables using a single MySQL query?
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- Insert with a Select query in MySQL
- Insert values in two tables with a single stored procedure call in MySQL
- A single MySQL query to select value from first table and insert in the second?
- How to count rows from two tables in a single MySQL query?
- A single MySQL query to find the highest and lowest among two tables?
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Insert multiple rows in a single MySQL query
- Insert multiple values in a temporary table with a single MySQL query?
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- How to insert multiple rows with single MySQL query?
- MySQL UNION SELECT and IN clause in a single query

Advertisements