Home >Backend Development >PHP Tutorial >How to Select Only Unique Dates from a MySQL Table?

How to Select Only Unique Dates from a MySQL Table?

Susan Sarandon
Susan SarandonOriginal
2024-12-03 11:49:10598browse

How to Select Only Unique Dates from a MySQL Table?

Selecting Unique Values from a MySQL Column

Suppose you have a MySQL table containing dates and product names. You need to retrieve unique dates from this table to avoid displaying duplicate entries. Here's a detailed guide to accomplish this task effectively:

The following script can be used to retrieve data from the table, but it includes duplicates:

$sql = mysql_query("SELECT * FROM buy ORDER BY Date");

To display only unique values, modify the script as follows:

$sql = mysql_query("SELECT DISTINCT(Date) AS Date FROM buy ORDER BY Date DESC");

The DISTINCT operator eliminates duplicate values from the selected column, in this case, 'Date'. The ORDER BY clause with DESC sorts the results in descending order, showing the most recent dates first.

By incorporating these adjustments into your script, the dates displayed will now be unique, providing you with a cleaner and more concise list of available dates.

The above is the detailed content of How to Select Only Unique Dates from a MySQL Table?. 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