Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting unique values from a column

Tags:

sql

php

mysql

People also ask

How do you get unique values from a database?

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.

How do I get a list of unique values from a table?

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.

How do I select a record without duplicates in one column in SQL?

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.

Can we use unique in select query?

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`