Jump to content

Combining (I.e. Adding) Order By Options?


galvin

Recommended Posts

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";

Link to comment
https://forums.phpfreaks.com/topic/269120-combining-ie-adding-order-by-options/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.