The unique values are fetched when we use the distinct keyword. SELECT DISTINCT returns only distinct (different) values. DISTINCT eliminates duplicate records from the table. DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
Excel has a very useful feature called remove duplicates which can be used on either the whole data set or a single column of data. Copy and paste the column of values which you want to see a list of unique values from to another location. Highlight this list of values.
If you want the query to return only unique rows, use the keyword DISTINCT after SELECT . DISTINCT can be used to fetch unique rows from one or more columns. You need to list the columns after the DISTINCT keyword.
DISTINCT or UNIQUE keyword in SQL is used with SELECT statement/query to select only unique records from a table, i.e. it eliminates all duplicate records from a table.
Use the DISTINCT operator in MySQL:
SELECT DISTINCT(Date) AS Date FROM buy ORDER BY Date DESC;
use
SELECT DISTINCT Date FROM buy ORDER BY Date
so MySQL removes duplicates
BTW: using explicit column names in SELECT
uses less resources in PHP when you're getting a large result from MySQL
Use this query to get values
SELECT * FROM `buy` group by date order by date DESC
The rest are almost correct, except they should order by Date DESC
SELECT DISTINCT(Date) AS Date FROM buy ORDER BY Date DESC;
DISTINCT
is always a right choice to get unique values. Also you can do it alternatively without using it. That's GROUP BY
. Which has simply add at the end of the query and followed by the column name.
SELECT * FROM buy GROUP BY date,description
Another DISTINCT
answer, but with multiple values:
SELECT DISTINCT `field1`, `field2`, `field3` FROM `some_table` WHERE `some_field` > 5000 ORDER BY `some_field`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With