Jump to content


Photo

Simple Group By/Order By not working


  • Please log in to reply
2 replies to this topic

#1 stmosaic

stmosaic
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 30 October 2006 - 01:40 AM

I have what should be a simple GROUP BY and ORDER BY that is only returning 1 item from each category rather than all of the records grouped together.

results=mysql_query("select * FROM db_name GROUP BY category ORDER BY ordernum LIMIT $rowstart,10");

The results I want to get are:
category1 blah, blah
category1 blah, blah
category1 blah, blah
category2 blah, blah
category2 blah, blah
category3 blah, blah
category3 blah, blah
category3 blah, blah

Right now, the number of rows per category ranges from 2 to 12 entries. Yet only one per category shows up. No error codes. I'm not sure why it won't work. I've looked through the forum & the tutorials, but most of them are more complex queries than what I'm doing. Can anyone see what I might be doing wrong?


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 30 October 2006 - 01:58 AM

It's doing what you told it to by grouping categories. You don't want to use "GROUP BY" at all in this case, and just use "ORDER BY category, ordernum".

#3 stmosaic

stmosaic
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 30 October 2006 - 02:08 AM

toplay, THANK YOU!!! That's the solution. My SQL background is VERY weak (and it shows!)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users