ORDER BY arithmetic expression involving several fields



  • I'm trying to get the ratings sorted. MySQL need a condition ORDER BY took the field with the assessments to divide it into the field with the number of votes.

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



  • To make such a request, you can use words after design. ORDER BYe.g.

    SELECT
      *
    FROM
      tbl
    ORDER BY
      marks / total DESC
    

    If there's a field total may take a value of 0, then a conditional choice may be added by means of a function in order to avoid division to zero. IF()

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



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2