

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 I see the CREATE TABLE statement of an existing MySQL table?
- How can we have multiple virtuals GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- MySQL join two tables?
- Concatenate two tables in MySQL with a condition?
- MySQL CREATE statement with KEY keyword
- Get table names using SELECT statement in MySQL?
- JavaScript Union of two objects
Advertisements