我有桌子,我存储博客帖子的费率(喜欢,不喜欢),我需要为每个帖子选择好恶的数量(当然还有加入桌面的博客:)) 表格如下:
+---------+---------+-----------+
| id_user | rate | id_blog |
+---------+---------+-----------+
| 1 | like | 1 |
| 2 | dislike | 1 |
| 3 | like | 1 |
| 6 | like | 1 |
.
.
.
最好的方法是什么?或者像这样意识到它是个坏主意?
答案 0 :(得分:4)
您可以使用:
SELECT
id_blog,
SUM(rate='like') AS like_count,
SUM(rate='dislike') AS dislike_count
FROM
blog_posts
GROUP BY
id_blog