• Teach Your Self With
             w3htmlschool.com

The MySQL UNION operator

The MySQL UNION operator is used to Concatenates results from multiple SELECTs . . It removes duplicate rows from result set. The MySQL UNION ALL ,show all the data from multiple table, the duplicate rows, if available
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

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
select name from address1
-> union
-> select name from address2;
Output
+------+
| name |
+------+
| web |
| Cat |
| nut |
| abc |
+------+
4 rows in set (0.00 sec)