Detail the use of UNION in MySQL

  • 2020-10-31 22:01:11
  • OfStack

UNION can be used if you want to select rows from several other tables or sets of rows from a single 1 table as a single result set row.

UNION can only be used in MySQL4.0 or above. This section explains how to use it.

Suppose there are two tables, potential and actual customer lists, and the supplier purchases consumables by merging the names and addresses from all three tables to create a single 1 mailing list. UNION provides a way to do this. Assume that the three tables have the following contents:


mysql> SELECT * FROM prospect;
+---------+-------+------------------------+
| fname | lname | addr     |
+---------+-------+------------------------+
| Peter | Jones | 482 Rush St., Apt. 402 |
| Bernice | Smith | 916 Maple Dr.   |
+---------+-------+------------------------+
mysql> SELECT * FROM customer;
+-----------+------------+---------------------+
| last_name | first_name | address    |
+-----------+------------+---------------------+
| Peterson | Grace  | 16055 Seminole Ave. |
| Smith  | Bernice | 916 Maple Dr.  |
| Brown  | Walter  | 8602 1st St.  |
+-----------+------------+---------------------+
mysql> SELECT * FROM vendor;
+-------------------+---------------------+
| company   | street    |
+-------------------+---------------------+
| ReddyParts, Inc. | 38 Industrial Blvd. |
| Parts-to-go, Ltd. | 213B Commerce Park. |
+-------------------+---------------------+

This does not matter if all three tables have different column names. The following query demonstrates how to select 1 down from 3 table names and addresses:


mysql> SELECT fname, lname, addr FROM prospect
-> UNION
-> SELECT first_name, last_name, address FROM customer
-> UNION
-> SELECT company, '', street FROM vendor;
+-------------------+----------+------------------------+
| fname    | lname | addr     |
+-------------------+----------+------------------------+
| Peter    | Jones | 482 Rush St., Apt. 402 |
| Bernice   | Smith | 916 Maple Dr.   |
| Grace    | Peterson | 16055 Seminole Ave. |
| Walter   | Brown | 8602 1st St.   |
| ReddyParts, Inc. |   | 38 Industrial Blvd. |
| Parts-to-go, Ltd. |   | 213B Commerce Park. |
+-------------------+----------+------------------------+

If you want to select all records, including duplicates, ask ALL for the first UNION keyword:


mysql> SELECT fname, lname, addr FROM prospect
-> UNION ALL
-> SELECT first_name, last_name, address FROM customer
-> UNION
-> SELECT company, '', street FROM vendor;
+-------------------+----------+------------------------+
| fname    | lname | addr     |
+-------------------+----------+------------------------+
| Peter    | Jones | 482 Rush St., Apt. 402 |
| Bernice   | Smith | 916 Maple Dr.   |
| Grace    | Peterson | 16055 Seminole Ave. |
| Bernice   | Smith | 916 Maple Dr.   |
| Walter   | Brown | 8602 1st St.   |
| ReddyParts, Inc. |   | 38 Industrial Blvd. |
| Parts-to-go, Ltd. |   | 213B Commerce Park. |
+-------------------+----------+------------------------+



Related articles: