Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select unique records by SQL

Tags:

sql

database

When I perform SELECT * FROM table I got results like below:

1 item1 data1 2 item1 data2 3 item2 data3 4 item3 data4 

As you can see, there are dup records from column2 (item1 are dupped). So how could I just get result like this:

1 item1 data1 2 item2 data3 3 item3 data4 

Only one record are returned from the duplicate, along with the rest of the unique records.

like image 496
Yinan Avatar asked Oct 29 '09 05:10

Yinan


People also ask

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.

How do I SELECT without duplicates 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.


1 Answers

With the distinct keyword with single and multiple column names, you get distinct records:

SELECT DISTINCT column 1, column 2, ... FROM table_name; 
like image 178
mjallday Avatar answered Oct 01 '22 22:10

mjallday