mysql - order by percentage of likes and return the next row -


i'm trying join 2 tables, image table , likes/dislikes table. need calculate percentage of likes each image , order percentage. find current item , return next item.

so i've broken couple of queries, first find percentage current image has.

then second i'm utterly confused:

select *,     sum(case when liked = '1' 1 else 0 end) liked,    sum(case when liked = '0' 1 else 0 end) disliked        images     left join         image_like il on i.image_id = il.like_image_id    (liked/liked + disliked) > ?        , image_site_id = ?    group i.image_id    order (liked/liked + disliked) desc    limit 1 

i'm getting error stating disliked not recognised in clause.

the reason because cannot use column alias in where @ same level select. but, logic on aggregated values anyway, belongs in having clause.

assuming liked takes on values 0 , 1, can write query as:

select i.*, sum(liked = 1) liked, sum(liked = 0) disliked images left join       image_like il      on i.image_id = il.like_image_id image_site_id = ? group i.image_id having avg(liked = 1) > ? order avg(liked = 1) desc limit 1 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -