我正在寻找一个查询,它将views
根据单个用户的id
.
这是表格的内容:
id | post_id | user_id | views
1 | 4 | 67 | 4
1 | 56 | 67 | 3
1 | 34 | 67 | 10
我想获取用户拥有的帖子视图总数
SELECT *,count(views) from users WHERE user_id = 67
结果必须是17
SELECT user_id, sum(views) FROM users WHERE user_id = 67 GROUP BY user_id
你想要SUM
运营商。
SELECT SUM(views) FROM users WHERE user_id = 67
只是SUM
意见:
简单的方法:
SELECT SUM(views)
FROM users
WHERE user_id = 67;
更复杂的方法:
SELECT user_id, SUM(views)
FROM users
WHERE user_id = 67
GROUP BY user_id;