php - How to divide 2 joined SQL columns? -
so im trying create highscore table "create own burger" application.
my sql query:
select save_burger.name, rating.totalscore, rating.totalvotes save_burger inner join rating on save_burger.ratingid=rating.ratingid order rating.totalscore desc
which returns:
|---------------------------------------| | name | totalscore | totalvotes | |burger1 | 11 | 3 | |burger2 | 6 | 2 | |burger3 | 5 | 5 | |burger4 | 2 | 1 | |burger5 | 0 | 0 | |---------------------------------------|
i use php divide totalscore , totalvotes:
if (($row["totalscore"] != 0) || ($row["totalvotes"] != 0)) { $totalratingscore = $row["totalscore"] / $row["totalvotes"]; echo substr($totalratingscore, 0, 3); } else { echo 0;
the code working fine. problem doesnt echo highest divided value first. though burger3 have average rating of 1, still topping burger4 average rating of 2.
what best choice? should divide sql query or there php fix? if so, how?
thanks in advance,
thomas
you need order ratio:
order rating.totalscore/rating.totalvotes desc
Comments
Post a Comment