View Full Version : ORDER by "%" ?

02-04-2010, 12:55 AM
Okay, I am trying to change my old rating with my new one. this time how I calculate top rated games is by how much people like it from dont like. so be is converted to a % in PHP, but how do I place that into a query?

my old query.

$sql_query = "SELECT * from games WHERE gamestatus = 1 ORDER BY rating DESC LIMIT 14";

this is the new rating, and I want to somehow do ORDER by highest %

$rate_yes = $row['rate_yes'];
$rate_no = $row['rate_no'];
$rate_t = $rate_yes + $rate_no;
$abcd = round($rate_yes / $rate_t * 100);

I making any sense, hope you can see what I'm trying to do.

02-04-2010, 01:24 AM
So basicly say I have 5 games

Game 1 = 80%
Game 2 = 21%
Game 3 = 45%
Game 4 = 66%
Game 5 = 12%

How would I 'ORDER by' in a query,

database fields are listed above, and you have to do the rounding out to get the %, but how can I order that list on the site with highest % to lowest? thanks

Len Whistler
02-04-2010, 01:26 AM
I would make this line more clear on the order it does the math.

$abcd = round($rate_yes / $rate_t * 100);


$abcd = round($rate_yes / $rate_t) * 100));

Now I don't know if the math is correct for your needs, it's just a syntax example to make it more readable.


02-04-2010, 01:59 AM
I ran this query, came out okay.. but just to be sure. on a large # of traffic. Is this safe to run through the database?

$sql_query = "SELECT * from games WHERE gamestatus = 1 AND (rate_yes > rate_no) ORDER BY (rate_yes / rate_no)*100 DESC LIMIT 14";

02-04-2010, 07:26 PM
I cant find any issues with the code. But you could have remove the % from the field data making it to a integer/float value and simply use the order by asc/desc

I cant see any specific reason to insert the % in to the DB, and if you want to update the field data you can use replace function in mysql.