The MySQL UNION operator​
Each SELECT statement within the UNION operator must have the same number of column in the result sets with similar data types.
SYNTAX
SELECT expression1, expression2, … expression_n
FROM tables
[WHERE conditions]
UNION [DISTINCT]
SELECT expression1, expression2, … expression_n
FROM tables
[WHERE conditions];
Table structure Used for Example explanation
mysql> desc address1;
+——-+————-+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+—————-+
| sno | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| email | varchar(40) | NO | | NULL | |
+——-+————-+——+—–+———+—————-+
3 rows in set (0.00 sec)
mysql> desc address2;
+——-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| sno | int(11) | NO | | NULL | |
| name | varchar(30) | NO | | NULL | |
| email | varchar(40) | NO | | NULL | |
+——-+————-+——+—–+———+——-+
Example for MySQL UNION operator
select name from address1
-> union
-> select name from address2;
Output of Above Example
+——+
| name |
+——+
| web |
| Cat |
| nut |
| abc |
+——+
4 rows in set (0.00 sec)