sql - MySQL: How to use alias to construct a new field? -


i trying use 2 aliases (like_count , dislike_count) construct new field (score) used order purposes. below current piece of query have not work. following error below query: "unknown column 'like_count' in 'field list". have looked similar question on so, have not been able find out solution. has idea how it?

select comment.id, comment.content,   sum(if(comment_reaction.type = 'like', 1, 0)) like_count, sum(if(comment_reaction.type = 'dislike', 1, 0)) dislike_count,  ((like_count + 1.9208) / (like_count + dislike_count) -      1.96 * sqrt((like_count * dislike_count) / (like_count + dislike_count) + 0.9604) /          (like_count + dislike_count)) / (1 + 3.8416 / (like_count + dislike_count)) score,  (select comment_reaction.type comment_reaction comment_reaction.person_id = :person_id , comment.id = comment_reaction.comment_id) my_reaction  comment left join comment_reaction on comment.id = comment_reaction.comment_id  comment.topic_id = :topic_id   group comment.id order score desc limit 0, 10 

wrap original query in derived table. add new column:

select dt.*,     ((like_count + 1.9208) / (like_count + dislike_count) -          1.96 * sqrt((like_count * dislike_count) / (like_count + dislike_count) + 0.9604) /              (like_count + dislike_count)) / (1 + 3.8416 / (like_count + dislike_count))     score  ( select      comment.id, comment.content,       sum(if(comment_reaction.type = 'like', 1, 0)) like_count,     sum(if(comment_reaction.type = 'dislike', 1, 0)) dislike_count,      (select comment_reaction.type comment_reaction     comment_reaction.person_id = :person_id     , comment.id = comment_reaction.comment_id) my_reaction,      comment     left join comment_reaction on comment.id = comment_reaction.comment_id      comment.topic_id = :topic_id      group comment.id ) dt order score desc 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -