Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SELECT DISTINCT in MYSQL

Been doing a lot of searching and haven't really found an answer to my MYSQL issue.

SELECT DISTINCT name, type, state, country FROM table

Results in 1,795 records

SELECT DISTINCT name FROM table

Results in 1,504 records

For each duplicate "name"... "type", "state", "country" aren't matching in each record.

Trying to figure out how to SELECT the associated row to the DISTINCT name, without checking them for being DISTINCT or not

like image 341
ZaneDeFazio Avatar asked Jun 22 '10 08:06

ZaneDeFazio


People also ask

How do I SELECT distinct data in MySQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.

How can I get distinct rows in MySQL?

Introduction to MySQL DISTINCT clause When querying data from a table, you may get duplicate rows. To remove these duplicate rows, you use the DISTINCT clause in the SELECT statement. In this syntax, you specify one or more columns that you want to select distinct values after the SELECT DISTINCT keywords.

How does SELECT distinct work?

The DISTINCT keyword in the SELECT clause is used to eliminate duplicate rows and display a unique list of values. In other words, the DISTINCT keyword retrieves unique values from a table.

Does SELECT distinct remove duplicates?

Introduction to SQL DISTINCT operator Note that the DISTINCT only removes the duplicate rows from the result set. It doesn't delete duplicate rows in the table. If you want to select two columns and remove duplicates in one column, you should use the GROUP BY clause instead.


2 Answers

SELECT name, type, state, country FROM table GROUP BY name;

should do the trick.

like image 68
cypher Avatar answered Oct 09 '22 09:10

cypher


If you want distinct name, you must decide which of the multiple values that may occur for each distinct name you want. For example, you may want minimals, or counts:

SELECT name, min(type), min(state), count(country) FROM table GROUP BY name

like image 31
František Žiačik Avatar answered Oct 09 '22 10:10

František Žiačik