If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Multiple distinct fields

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-09, 21:04
warrentr warrentr is offline
Registered User
 
Join Date: Sep 2009
Posts: 2
Multiple distinct fields

Hello,
Take the following table for example:
-----------------------------------
----ID-----------|-----YEAR-------
| 0 ------------------ 2008
| 1 ------------------ 2008
| 2 ------------------ 2008
| 3 ------------------ 2009
| 4 ------------------ 2009
| 5 ------------------ 2009
| 6 ------------------ 2009
-----------------------------------

What I need to do is select one full row per year. That is with both ID and YEAR, and just any old row (ID doesn't matter).

select distinct year from table;

works, but does not include the ID. And:

select distinct year, id from table;

Has duplicates of year...

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 09-11-09, 00:52
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Try this ...

Code:
SELECT year, id FROM table GROUP BY year
Reply With Quote
  #3 (permalink)  
Old 09-11-09, 00:53
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Oh I forgot to add ... if you want to get the lowest or the highest ID for the year ... just change id to MAX(id) or MIN(id)
Reply With Quote
  #4 (permalink)  
Old 09-11-09, 01:42
warrentr warrentr is offline
Registered User
 
Join Date: Sep 2009
Posts: 2
Wow, well i swear I tried that but apparently not

Thanks. group by year did the trick.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On