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