Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL: SELECT Method - but don't show duplicates / GROUP or DISTINCT?

How can I select and don't show duplicates? Actually, it's showing like that: apple | apple | apples | apple

This is my code:

$search = $_GET['q'];
$query = "SELECT * FROM query WHERE searchquery LIKE '%$search%' AND searchquery <> '$search'"; 
like image 626
elmaso Avatar asked Apr 05 '10 23:04

elmaso


1 Answers

You already said the magic word: DISTINCT.

SELECT DISTINCT columnname
FROM query
WHERE ....

Note that it probably won't work if you use SELECT DISTINCT * because when you select * that means select all columns, including columns which have a unique constraint such as the primary key. Only select the columns you need - stay away from * in general, and especially so when using DISTINCT.

like image 127
Mark Byers Avatar answered Sep 28 '22 01:09

Mark Byers