我有一个表,我在其中存储博客文章(喜欢的,不喜欢的)的速率,我需要为每个帖子选择喜欢的和不喜欢的计数(当然,加入到表中的博客:)
桌子看起来像这样:
+---------+---------+-----------+
| id_user | rate | id_blog |
+---------+---------+-----------+
| 1 | like | 1 |
| 2 | dislike | 1 |
| 3 | like | 1 |
| 6 | like | 1 |
.
.
.
怎么做才是最好的呢?或者像这样意识到这是个坏主意?
最佳答案
您可以使用:
SELECT
id_blog,
SUM(rate='like') AS like_count,
SUM(rate='dislike') AS dislike_count
FROM
blog_posts
GROUP BY
id_blog
关于mysql - 选择值(value)计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21311356/