mysql queries question...order by division of two column

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

mysql queries question...order by division of two column

Post by dmmh »

in order to be able to use proper sorting functionality on my site I need my query to be modified to calculate the division of two INT values in a returned row

I need this for rating functionality, so if I would simply ORDER BY (field_rating), this wouldnt be precise enough, as it may have a value of 10 with 10 votes, which averages 1 and another row might be 9, but with 1 votes, which would mean the first scenario would be shown first, but it should be the other way around...if you know what I mean :p

my query now:

$query = 'SELECT * FROM comments'.
" INNER JOIN users_table".
" ON (users_table.userid = comments.added_by)".
" WHERE comments.mid = '$mid'".
" ORDER BY comments.$sort $order".
" LIMIT $from, $max_results";
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

Why does it matter how many votes?
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

so one can compute a average? :dork:
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

shouldnt the rating already be an average?
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

and this is handy how?
i could do it like that, but its more cumbersome to compute a new average when a new vote is cast (because you'd first need to multiply by numbers of votes, then sum it up, the devide again, now I just sum and divide)

im sure it can be done
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

Well now that you told me you are storing the sum and not the rating that helps :p

Just do this:
if ($sort == 'rating') {
$query = 'SELECT * FROM comments'.
" INNER JOIN users_table".
" ON (users_table.userid = comments.added_by)".
" WHERE comments.mid = '$mid'".
" ORDER BY comments.rating/comments.total $order".
" LIMIT $from, $max_results";
}
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

thanks, figured it would be something simple nowhere to be found in some docs lol

didnt notice I forgot to mention I just use a sum, thx for the help :up:
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Post by bitWISE »

n/p.

I didn't really know you could do it so I fired up phpMyAdmin and tested it out.
Post Reply