[英]SQL: getting COUNT for each N
Is there a way to count the times every unique like_user_id
is in the rows? 有没有一种方法可以计算行中每个唯一的
like_user_id
的次数?
Adding onto this; 添加到此; is there a way to output this to a table with
like_user_id
and count
? 有没有一种方法可以将其输出到具有
like_user_id
和count
的表中?
I've already tried the following, but this only does it for like_user_id
= 2
. 我已经尝试了以下方法,但这仅适用于
like_user_id
= 2
。
Here's the table I have. 这是我的桌子。 link
链接
SELECT *, COUNT(`like_user_id`) FROM `xf_liked_content`
WHERE `xf_liked_content`.`like_user_id` = 2;
this is actually very simple. 这实际上非常简单。
All you need to do is to group them by the term like_user_id
and select it, and its count! 您需要做的就是将它们按术语
like_user_id
然后选择它,然后对其计数! Here is a full example: 这是一个完整的示例:
SELECT like_user_id, count(like_user_id) FROM xf_liked_content GROUP BY like_user_id
There are a lot of constraints on how to use GROUP BY
, for instance AFAIK, you cannot SELECT
a term that is not on your GROUP BY
list, but this is no issue in your case. 如何使用
GROUP BY
存在很多限制,例如AFAIK,您不能SELECT
不在GROUP BY
列表中的术语,但这对您来说不是问题。 For more information on the group by
statement, please check https://www.w3schools.com/sql/sql_groupby.asp . 有关
group by
声明group by
更多信息,请检查https://www.w3schools.com/sql/sql_groupby.asp 。
Enjoy programming! 享受编程!
Try to use this code: 尝试使用以下代码:
select 'like_user_id', count(*) as cnt
from 'xf_liked_content'
group by 'like_user_id'
Or this, diffenetly depends on your DBMS: 或者,这完全取决于您的DBMS:
select "like_user_id", count(*) as cnt
from "xf_liked_content"
group by "like_user_id"
Or next code: 或下一个代码:
select like_user_id, count(*) as cnt
from xf_liked_content
group by like_user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.