Jump to content

GROUP BY


jaymc

Recommended Posts

I have a table which consists of usernames along with there gender and a timestamp, e.g

 

Jamie - Male - 121123123

Sally - Female - 342342342

Amy = Female - 34535353

Derek = Male - 234342424

 

I want to pull out 5 males and 5 females in order of timestamp

 

I could easily use a GROUP BY and have 2 queries dig out the males and then females

 

However, to save resources I want to cramp this into one query

 

Any ideas how?

 

I was going to use PHP after pulling out the data to display 5 males in order of time stamp, then when the counter hits 5 ignore everything else apart from females, again until the counter hits 5

 

Is there a better way to do this?

 

Thanks

Link to comment
Share on other sites

  • 2 weeks later...

A couple days late, searching for my own problem, but if someone comes by this ever:

 

Simply do this:

 

SELECT * FROM U_G_T WHERE gender='male' ORDER BY timestamp DESC UNION SELECT * FROM U_G_T WHERE gender='female' ORDER BY timestamp DESC

 

Or something simillar! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.