mysql - SQL error say column from COUNT does not exisit -
this question has answer here:
- sql query left join issue 2 answers
select c.review, m.category, u.username, i.item, i.item_id, m.cat_id, count(rv.review_id) totalcount reviews c left join review_vote rv on c.review_id = rv.review_id left join users u on u.user_id = c.user_id left join items on i.item_id = c.item_id left join master_cat m on m.cat_id = i.cat_id length(c.review) > 50 , m.category = 'movies' , totalcount > 2 group rv.review_id order rand() limit 1
i error:
#1054 - unknown column 'totalcount' in 'where clause'
i selecting column right away, how come doesn't exist?
i trying random select review listed (or voted on 2 or more times) in in table rv
. not sure if using group by
correctly?
with aggregate columns (like count()
), must use having
clause place condition on them:
select c.review, m.category, u.username, i.item, i.item_id, m.cat_id, count(rv.review_id) totalcount reviews c left join review_vote rv on c.review_id = rv.review_id left join users u on u.user_id = c.user_id left join items on i.item_id = c.item_id left join master_cat m on m.cat_id = i.cat_id length(c.review) > 50 , m.category = 'movies' group rv.review_id having count(rv.review_id) > 2 -- added line! order rand() limit 1
some databases (including mysql) allow use column alias, eg having totalcount > 2
, using expression works databases. ie mysql (at least), code as:
.... group rv.review_id having totalcouunt > 2 ...
but wouldn't portable other databases.
Comments
Post a Comment