是否有人知道如何计算值在单个列中显示的总次数。
例如我的表格如下:
user_id | liked_id | likes
3 1 1
4 1 1
我想要做的是计算like_id匹配的喜欢列的总数。
所以likes_id 1有两个喜欢?
有人可以告诉我怎么做这个吗?function count_likes() {
global $connection;
global $profile_id;
$query = "SELECT likes, count(*) FROM ptb_likes GROUP BY liked_id";
$count_likes_set = mysql_query($query, $connection);
confirm_query($count_likes_set);
return $count_likes_set;
}
答案 0 :(得分:3)
试试这个:
SELECT liked_id, SUM(likes) FROM ptb_likes GROUP BY liked_id
答案 1 :(得分:1)
您应该按liked_id
SELECT liked_id, count(*) totalLikes
FROM ptb_likes
GROUP BY liked_id
答案 2 :(得分:0)
如果你只想要like_id为1的行数,
SELECT liked_id, COUNT(likes) FROM ptb_likes WHERE liked_id = 1;
如果你想要每个like_id的频率:
SELECT liked_id, COUNT(likes) FROM ptb_likes GROUP BY liked_id;
作为注释:不要使用SUM,因为它会添加like_id的所有值,如果你的like_id大于1,那么你的计数将是错误的。改为使用计数。