View Full Version : Can't use select distinct properly!! Help!!
07-04-2011, 06:32 AM
Hi Humans! I'm just a damned right novice and I can't even use sql properly. So, some help would be appreciated. How do I select all the values from a database, bit like this: selecting all columns from a table where only I want distinct values for one column only. I've got:
SELECT DISTINCT test_username FROM test_records WHERE test_id = 'defaultvalue' ORDER BY test_id DESC LIMIT 5;
Thanks, any help would be appreciated. :)
07-04-2011, 03:51 PM
Get rid of the DISTINCT.
SELECT test_username FROM test_records WHERE test_id = 'defaultvalue' ORDER BY test_id DESC LIMIT 5;
07-04-2011, 08:02 PM
when you want distinct values from one column then you have to decide which row you want when there are multiple occurrences of that value from that column.
As an example:
City, time, temp
Niagara, 2 p.m., 75
Niagara, 3 p.m., 80
Buffalo, 2 p.m., 81
Chicago, 1 p.m., 92
which of those two rows do you want for Niagara?
DISTINCT is not a function, it works across all columns from your table, not on a single column. So all rows in the example are DISTINCT from one another.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.