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!