views:

29

answers:

1

Hello, I want order a selection of items by the rating. There are 2 fields: rateup and ratedown. I use these fields to calculate a number and order the selection on that number. Can that be done in mysql? I have a querylike this, but this doesn't work:

SELECT * FROM table ORDER BY (((9/rateup+ratedown)*rateup)+1) DESC

How can I make this work or is this impossible?

+2  A: 
Select *, (((9/rateup+ratedown)*rateup)+1) As Ord from Table WHERE published = 1 Order By Ord Desc

added Where Clause as requested.

websch01ar
Thanks for the fast reply! One question: Where do I place the WHERE statements? Like: WHERE published = 1 AND ...
Bert
Select *, (((9/rateup+ratedown)*rateup)+1) As Ord from Table WHERE (put criteria here) Order By Ord, dEsc
websch01ar
It looks like it doesn't do the calculation:#1 has rateup:1, ratedown 0 (calculated mark = 10)#2 has rateup:2, ratedown 1 (calculated mark = 7)But in the result of this query (when DESC) #2 is placed above #1. While 10 is greater than 7. :S
Bert
It's because `/` has higher precedence than `+`... Your algorithm boils down to `((((9 / rateup) + ratedown) * rateup) + 1`, which is calculated as `10` for row #1, and `12` for row #2 as well (`9/2 == 4.5, 4.5 + 1 = 5.5, 5.5 * 2 == 11, 11 + 1 == 12`)...
ircmaxell
a, your right, didn't see that. Thanks a lot, now it works as expected!
Bert
Always good to use () as these can encapsulate the inner math.
websch01ar