PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 13, 2022

[FIXED] How to get both like and dislike count of the post in single query using cake php or php

 February 13, 2022     cakephp, cakephp-3.0, mysql, php, sql     No comments   

Issue

I have researched a day but I not get the solution.if it is possible to get he likes and dislike count of all post using single CakePHP query table structure

id user_id  posts_id  like_and_dislike
1    1         1          1
2    2         1          1
3    4         1          0
4    5         2          1

Here we need to get the like and dislike count of all posts that is for post 1 like -> 2 and dislike -> 1 And for post - 2 like -> 1 and dislike -> 0 need to list the count.please any one help on this query if it not possible please help in php query


Solution

I'm guess you need this

SELECT post_id, 
COUNT( CASE WHEN like_and_dislike = 1 THEN 1 END ) AS like_cnt, 
COUNT( CASE WHEN like_and_dislike = 0 THEN 1 END ) AS dislike_cnt 
FROM your_table
GROUP BY post_id
-- ORDER BY something
-- LIMIT x, y


Answered By - Oto Shavadze
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing