Reputation: 871
I have table, where I store rate of blog posts (liked, disliked) and I need to select count of likes and dislikes for each post (and of course JOIN it on table with blogs :) ) The table look like this:
+---------+---------+-----------+
| id_user | rate | id_blog |
+---------+---------+-----------+
| 1 | like | 1 |
| 2 | dislike | 1 |
| 3 | like | 1 |
| 6 | like | 1 |
.
.
.
How is the best to to this? Or is bad idea to realize it like this?
Upvotes: 2
Views: 93
Reputation: 37365
You can use:
SELECT
id_blog,
SUM(rate='like') AS like_count,
SUM(rate='dislike') AS dislike_count
FROM
blog_posts
GROUP BY
id_blog
Upvotes: 4