Trying to sort by rating. It is necessary that the MySQL condition ORDER BY takes a field with estimates divided it into a field with the number of voters.

Is it possible to put such a condition? What would such a design look like?

  • And yes everything turned out to be simple, just like I wrote, you need to write the ORDER BY condition first field / second field - alexsis20102
  • 3
    So how few people understood what from the question and you yourself have already found what you were looking for - can it be easier to delete the question? - Alexey Shimansky

1 answer 1

To compose such a query, you can use expressions after the ORDER BY , for example

 SELECT * FROM tbl ORDER BY marks / total DESC 

If the total field can take the value 0, then in order to avoid division by zero, you can add a conditional choice using the IF() function

 SELECT * FROM tbl ORDER BY IF(total = 0, 0, marks / total) DESC