
- 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
Using CREATE TABLE AS statement with UNION of two tables in MySQL
For this, you can use UNION. Let us first create a table −
mysql> create table DemoTable1(FirstName varchar(1000)); Query OK, 0 rows affected (1.22 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('John'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | John | +-----------+ 1 row in set (0.02 sec)
Here is the query to create second the table −
mysql> create table DemoTable2(FirstName varchar(100)); Query OK, 0 rows affected (0.81 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values('Chris'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | +-----------+ 1 row in set (0.00 sec)
Following is the query for CREATE TABLE AS statement and displaying the union of two or more tables in it −
mysql> create table DemoTable3 AS ( select FirstName, 'DemoTable1' AS `TABLE_NAME` from DemoTable1) union ( select FirstName, 'DemoTable2' AS `TABLE_NAME` from DemoTable2); Query OK, 2 rows affected (1.08 sec) Records: 2 Duplicates: 0 Warnings: 0
Display all the records from the table DemoTable3 −
mysql> select *from DemoTable3;
This will produce the following output −
+-----------+--------------+ | FirstName | TABLE_NAME | +-----------+--------------+ | John | DemoTable1 | | Chris | DemoTable2 | +-----------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to create a new table from merging two tables with MySQL union?
- Merge two tables with union in MySQL?
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- How ANALYZE TABLE statement helps in maintaining the MySQL tables?
- Select the table name as a column in a UNION select query with MySQL?
- What is the use of OPTIMIZE TABLE statement in maintaining the MySQL tables?
- What is the use of CHECK TABLE statement in maintaining the MySQL tables?
- How we have multiple stored GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- How can we have multiple virtuals GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- Get table names using SELECT statement in MySQL?
- Concatenate two tables in MySQL with a condition?
- Can we use “When” as column name in CREATE TABLE statement?
- MySQL CREATE statement with KEY keyword
- How to create a temporary MySQL table in a SELECT statement without a separate CREATE TABLE?

Advertisements