Using Mysql Alias in WHERE

Discussion in 'MySQL' started by ridesign, Jun 13, 2009.

  1. #1
    I am trying to execute the following query but I get unknown column for rating_score for the WHERE clause.

    SELECT SUM(ratings.rating_rating) AS rating_score, posts.ID, posts.post_title FROM posts LEFT JOIN ratings ON ratings.rating_postid = posts.ID WHERE rating_score >= 1 GROUP BY posts.ID ORDER BY posts.post_date DESC LIMIT 5
    Code (markup):
     
    ridesign, Jun 13, 2009 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Use HAVING

    SELECT SUM(ratings.rating_rating) AS rating_score, posts.ID, posts.post_title FROM posts 
    LEFT JOIN ratings ON ratings.rating_postid = posts.ID 
    GROUP BY posts.ID 
    HAVING rating_score >= 1 
    ORDER BY posts.post_date DESC LIMIT 5
    Code (markup):
     
    mwasif, Jun 13, 2009 IP