Database Reference
In-Depth Information
▼
Analysis
The first
SELECT
retrieves all products with a price of no more than
5
. The
second
SELECT
uses
IN
to find all products made by vendors
1001
and
1002
.
To combine these two statements, do the following:
▼
Input
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
UNION
SELECT vend_id, prod_id, prod_price
FROM products
WHERE vend_id IN (1001,1002);
▼
Output
+---------+---------+------------+
| vend_id | prod_id | prod_price |
+---------+---------+------------+
| 1003 | FC | 2.50 |
| 1002 | FU1 | 3.42 |
| 1003 | SLING | 4.49 |
| 1003 | TNT1 | 2.50 |
| 1001 | ANV01 | 5.99 |
| 1001 | ANV02 | 9.99 |
| 1001 | ANV03 | 14.99 |
| 1002 | OL1 | 8.99 |
+---------+---------+------------+
▼
Analysis
The preceding statements are made up of both of the previous
SELECT
state-
ments separated by the
UNION
keyword.
UNION
instructs MariaDB to execute
both
SELECT
statements and combine the output into a single query result set.
As a point of reference, here is the same query using multiple
WHERE
clauses
instead of a
UNION
:
▼
Input
SELECT vend_id, prod_id, prod_price
FROM products
WHERE prod_price <= 5
OR vend_id IN (1001,1002);