jaymc Posted July 14, 2007 Share Posted July 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
solinent Posted July 23, 2007 Share Posted July 23, 2007 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! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 23, 2007 Share Posted July 23, 2007 JOIN can do the similar effect, however Join/Union dramatically kill your speed especially with that bugger * for all fields, try selecting what you need on a complex query Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.