Home >Database >Mysql Tutorial >How to Select Distinct Values from Multiple Columns in MySQL?
MySQL Query to Fetch Distinct Values of Multiple Columns
In MySQL, the SELECT DISTINCT statement can be used to retrieve distinct values from one or more columns. Here's a solution to the problem of selecting distinct values of multiple columns (a, b, c, and d) from a table:
SELECT DISTINCT a, b, c, d FROM my_table;
This query will return a set of rows where each row represents a unique combination of distinct values for the four columns. It does not group the results, unlike the second query in the question.
To clarify the expected output, the distinct values for each column will be displayed separately, rather than distinct combinations of values across all columns. Here's an example:
Suppose we have the following data in the my_table:
a | b | c | d |
---|---|---|---|
1 | 2 | 3 | 4 |
1 | 2 | 3 | 5 |
6 | 7 | 8 | 9 |
1 | 2 | 3 | 6 |
10 | 11 | 12 | 13 |
Executing the provided query would yield the following distinct values:
a | b | c | d |
---|---|---|---|
1 | 2 | 3 | 4 |
6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 |
As you can see, the distinct values of columns a, b, c, and d are listed separately, satisfying the requirement of this specific use case.
The above is the detailed content of How to Select Distinct Values from Multiple Columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!