raku Posted May 30, 2008 Share Posted May 30, 2008 Hey, Is there a way to order by a second variable if the first variable is equal for two rows? For example, if I'm trying to output a list of posts in the order of most comments, but two of the posts have the same number of comments, I'd like the post with the higher view count to be listed first. Thanks. Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 30, 2008 Share Posted May 30, 2008 ... order by comments, views desc ... ought to do it Quote Link to comment Share on other sites More sharing options...
blirette Posted May 30, 2008 Share Posted May 30, 2008 SELECT * FROM table ORDER BY number_comments, view_count I think GROUP BY could help...but maybe not. Depends on how your DB is setup. Good luck! Quote Link to comment Share on other sites More sharing options...
raku Posted May 31, 2008 Author Share Posted May 31, 2008 Thanks for the help guys! I had to do something like this: SELECT * FROM table ORDER BY number_comments DESC, view_count DESC It didn't seem to work without the DESC in there. 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.