mysql query to count of posts, comments and likes & dislikes with multiple joins

18 Views Asked by At

I'm trying to get posts with count of likes_dislikes and comments. I have six tables:

  1. users
  2. categories
  3. posts
  4. likes_dislikes
  5. comments
  6. likes_dislikes_types

Tables 1,2 are parents and 4,5 are childs of table 3-posts and table no 6-likes_dislikes_types is also parent of table no 4-likes_dislikes.

schema of 1- users

u* (users)

| u_id | u_name|
|----- |-------|
|  1   | arslan|

schema of 2- categories

c* (categories)

| c_id | c_name|
|----- |-------|
|  1   | post  |
|  2   | news  |

schema of 3- posts

p* (posts)
p_c_id F.K (2- categories)
p_u_id F.K (1- users)

| p_id | p_details|p_c_id | p_u_id |
|----- |----------|-------|--------|
|  1   | testing  |  1    |   1    |
|  2   | testing2 |  1    |   1    |
|  3   | testing3 |  2    |   1    |

schema of 4- likes_dislikes

ld* (likes_dislikes)
ldt* (likes_dislikes_types) table no. 6

ld_p_id F.K (3- posts)
ld_ldt_id F.K (6- likes_dislikes_types)

| ld_id| ld_ldt_id  |  ld_p_id |
|----- |------------|----------|
|  1   |    1       |     1    |   
|  2   |    1       |     1    |   
|  3   |    2       |     2    | 
|  4   |    2       |     3    | 
|  5   |    3       |     4    | 
|  6   |    4       |     5    | 

schema of 5- comments

com* (comments)
p_co_parent_id* for reply of comments

com_p_id F.K (3- posts)
com_parent_id self key (for replies of comments)

|com_id |com_details| com_parent_id |com_p_id |
|------ |-----------|---------------|---------|
|   1   | comment1  |      0        |    1    |

schema of 6- likes_dis

ldt* (likes_dislikes_types)

| ldt_id | ldt_type|
|------- |---------|
|   1    |  like   |
|   2    |  love   |
|   3    |  haha   |
|   4    |  wao    |
|   5    |  sad    |
|   6    |  angry  |

I've write query to get records. It's gives right results expected except wrong comments count. comments table have only one record for p_id =1 and null for other posts i.e 2,3 and so on. the query gave 10 comment count for p_id =1 and and 1 for all other posts.

problems with the query

  1. wrong comment count
  2. I required total comment count of post and sum of comments and replies (comments +replies) separately

my query is here.

SELECT *,
COUNT(`ld_ldt_id`) AS `total_likes_dislikes`,
COUNT( CASE WHEN `ld_ldt_id` = 1 THEN 1 END ) AS `like_count`, 
COUNT( CASE WHEN `AAld_ldt_id` = 2 THEN 2 END ) AS `love_count`, 
COUNT( CASE WHEN `ld_ldt_id` = 3 THEN 3 END ) AS `haha_count`, 
COUNT( CASE WHEN `ld_ldt_id` = 4 THEN 4 END ) AS `wao_count`, 
COUNT( CASE WHEN `ld_ldt_id` = 5 THEN 5 END ) AS `sad_count`, 
COUNT( CASE WHEN `ld_ldt_id` = 6 THEN 6 END ) AS `angry_count`,  
COUNT(`c1`.`com_p_id`) AS `comments_count`
FROM `posts`
INNER JOIN `categories` ON `c_id` = `p_c_id`
INNER JOIN `users` ON `u_id` = `p_u_id`
LEFT JOIN `likes_dislikes` ON `p_id` = `ld_p_id`
LEFT JOIN `comments` AS `c1` ON `p_id` = `c1`.`com_p_id`
LEFT JOIN `comments` AS `c2` ON `c2`.`com_id` = `c2`.`com_parent_id`
GROUP BY `p_id`
ORDER BY `p_id` ASC

what to do to get right answer?

0

There are 0 best solutions below