Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2011, 06:32 AM   PM User | #1
think123
New Coder

 
Join Date: Jun 2011
Posts: 59
Thanks: 3
Thanked 0 Times in 0 Posts
think123 is an unknown quantity at this point
Exclamation Can't use select distinct properly!! Help!!

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:
Code:
SELECT DISTINCT test_username FROM test_records WHERE test_id = 'defaultvalue' ORDER BY test_id DESC LIMIT 5;
Thanks, any help would be appreciated.
think123 is offline   Reply With Quote
Old 07-04-2011, 03:51 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
Get rid of the DISTINCT.

Code:
SELECT test_username FROM test_records WHERE test_id = 'defaultvalue' ORDER BY test_id DESC LIMIT 5;
sunfighter is offline   Reply With Quote
Old 07-04-2011, 08:02 PM   PM User | #3
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:43 AM.


Advertisement
Log in to turn off these ads.