mysql - count associate rows in multiple tables with left join -
i'm trying select posts associate numbers of comments , likes.
this query
select `waller_posts`.*, count(waller_comments.id) num_comments, count(waller_likes.id) num_likes `waller_posts` left bring together `waller_comments` on `waller_comments`.`post_id` = `waller_posts`.`id` left bring together `waller_likes` on `waller_likes`.`post_id` = `waller_posts`.`id` `wall_id` = 1 , `wall_type` = "user" grouping `waller_posts`.`id` when add together sec left join in case of likes, results of num_comments , num_likes came wrong. how can perform kind of query?
the query builds give every possible combination of comments , likes on post.
probably easiest utilize count(distinct...) :-
select `waller_posts`.*, count(distinct waller_comments.id) num_comments, count(distinct waller_likes.id) num_likes `waller_posts` left bring together `waller_comments` on `waller_comments`.`post_id` = `waller_posts`.`id` left bring together `waller_likes` on `waller_likes`.`post_id` = `waller_posts`.`id` `wall_id` = 1 , `wall_type` = "user" grouping `waller_posts`.`id` note query relying on feature of mysql cause error in flavours of sql. flavours of sql need list non aggregate columns in grouping clause.
mysql sql
No comments:
Post a Comment