Home >Database >Mysql Tutorial >How to Select Only Unique Values from a MySQL Column?

How to Select Only Unique Values from a MySQL Column?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-14 08:52:42990browse

How to Select Only Unique Values from a MySQL Column?

Extracting Unique Values in MySQL Columns

MySQL databases often contain columns with repeated entries. For example, a sales table might list the same product multiple times on the same day. This redundancy complicates data analysis and efficient storage. This guide shows how to efficiently select only the unique values from such a column.

The Solution: Using DISTINCT

The DISTINCT operator provides a straightforward solution. It ensures that only one instance of each unique value is returned in the query result.

Let's illustrate with a sample table:

Date Product
2011-12-12 azd
2011-12-12 yxm
2011-12-10 sdx
2011-12-10 ssdd

A typical SELECT query would return all rows:

Date Product
2011-12-12 azd
2011-12-12 yxm
2011-12-10 sdx
2011-12-10 ssdd

Notice the repeated dates. To retrieve only the unique dates, use DISTINCT:

<code class="language-sql">SELECT DISTINCT Date AS Date
FROM buy
ORDER BY Date DESC;</code>

This yields:

Date
2011-12-12
2011-12-10

As shown, the duplicates are removed, resulting in a concise list of unique dates from the buy table. This optimized query focuses on distinct values, improving data analysis accuracy and efficiency.

The above is the detailed content of How to Select Only Unique Values from a MySQL Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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