galvin Posted October 5, 2012 Share Posted October 5, 2012 I have a table with (among others) two columns for... ranking (always a number, always positive) adjustment (always a number, either positive or negative) Sometimes I want to order the query results by ranking (in ascending order), so that is accomplished easy enough by doing... $sql = "SELECT * from players ORDER BY ranking ASC"; But sometimes, I want to order the query results by the ranking PLUS the adjustment. For example... item 1: ranking = 3, adjustment =0 item 2: ranking = 5, adjustment = -3 Using my first simple query, item 1 would come first and item 2 would come second (because 3 is less than 5). Using my desire for ranking PLUS adjustment, item 2's ranking would "become" 2 because 5 plus -3 = 2 and therefore I would want to get item 2 back FIRST and then item 1 (since 2 is less than 3). Assuming my jibberish above makes sense, is there a way to do this "addition" in the MYSQL query itself? Something like the following (which doesn't work, but should give you the gist of what I want)... $sql = "SELECT * from players ORDER BY (ranking + adjustment) ASC"; Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2012 Share Posted October 5, 2012 ORDER BY (ranking + adjustment) That should be OK. Define "doesn't work" Quote Link to comment Share on other sites More sharing options...
galvin Posted October 5, 2012 Author Share Posted October 5, 2012 Barand, thank you for letting me know that should work. I stuck with it and eventually found another error in my code which was causing the problems. Anyway, thanks again for confirming it should have worked (it did). 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.