Database Reference
In-Depth Information
Tip
Retrieving Unknown Columns There is one big advantage to using wildcards. As you
do not explicitly specify column names (because the asterisk retrieves every column), it
is possible to retrieve columns whose names are unknown.
As you have seen,
SELECT
returns all matched rows. But what if you do not
want every occurrence of every value? For example, suppose you want the
vendor ID of all vendors with products in your
products
table:
▼
Input
SELECT vesnd_id
FROM products;
▼
Output
+---------+
| vend_id |
+---------+
| 1001 |
| 1001 |
| 1001 |
| 1002 |
| 1002 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1005 |
| 1005 |
+---------+
The
SELECT
statement returned 14 rows (even though only four vendors are
in that list) because 14 products are listed in the
products
table. So how
could you retrieve a list of distinct values?
The solution is to use the
DISTINCT
keyword, which, as its name implies,
instructs MariaDB to return only distinct values.
▼
Input
SELECT DISTINCT vend_id
FROM products;