Home > Database > Mysql Tutorial > How to Select Distinct Values from Multiple Columns in MySQL?

How to Select Distinct Values from Multiple Columns in MySQL?

Patricia Arquette
Release: 2024-12-27 02:21:13
Original
749 people have browsed it

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;
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template