MySQL Sorting Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gamernaveen
    New Member
    • Oct 2007
    • 28

    #1

    MySQL Sorting Question

    Hello everyone ! ,
    I am working on a rating field in my database. Basically , my rating formula goes like this
    Code:
    RATING = TOTAL POINTS / TOTAL RATINGS
    Ofcourse , its the average rating. I store the total points and total ratings in a single field called 'catrating' (like 4/2 i.e points=4 ratings=2) and explode it with php to get required values. What am looking for is , is there an easy MySQL inbuilt function to sort the ratings ( Like sorting numbers) like these directly in the query with some REGEX and AVG , so that all the top rated stuff gets sorted in the DESC order ?

    Thanks so much !
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Hi,

    Why not store the two values in separate fields?

    This would then enable you to more accurately use the MySQL 'ORDER BY' clause.

    The clause is simple enough ORDER BY tbl.field ASC/DESC. you can even tack orders together thus ordering on multiple columns.

    I would have though this was the easiest way to go.

    I admit this may involve a change in your existing code but it's going to make this job easier and give you greater flexibility in the future.

    If you have a good, technical reason for not doing this then post it here as I can think of one - other than potential headache of updating existing code, and that may not be an issue for you depending on how much code there is.

    Cheers
    nathj

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I would agree with nathj.

      Storing two values in a single field should never be done. It goes against one of the most basic *rules* of relational database design. It does nothing but cause problems (as you are experiencing).

      The best way to do this would be to separate the two values and have MySQL sort the values using ORDER BY.

      Comment

      • gamernaveen
        New Member
        • Oct 2007
        • 28

        #4
        ok ty guys , i'll make 2 fields

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by gamernaveen
          ok ty guys , i'll make 2 fields
          Smart decision.

          If you get stuck or need any further help then just give us a shout.

          Cheers
          nathj

          Comment

          Working...