使用相同用户ID对列进行SQL计数
我有两张桌子。表1包含满足特定标准的内容id。表2包含内容id、内容和相关用户id。它们共享一个内容id字段。我想列出表1中谁的条目最多 范例 表1 内容_id{1,2,3,4,5,6} 表2 内容id |用户id{1 | 2,2 | 3,3 | 2,4 | 1,5 | 3,6 | 2} 预期结果 用户2有3个条目 用户3有2个条目 用户1有1个条目使用相同用户ID对列进行SQL计数,sql,Sql,我有两张桌子。表1包含满足特定标准的内容id。表2包含内容id、内容和相关用户id。它们共享一个内容id字段。我想列出表1中谁的条目最多 范例 表1 内容_id{1,2,3,4,5,6} 表2 内容id |用户id{1 | 2,2 | 3,3 | 2,4 | 1,5 | 3,6 | 2} 预期结果 用户2有3个条目 用户3有2个条目 用户1有1个条目 我想我需要通过content\u id将两个表内部连接起来,然后以某种方式使用COUNT或类似的方法?您是否正在寻找一个简单的分组依据 编辑: 如
我想我需要通过content\u id将两个表内部连接起来,然后以某种方式使用COUNT或类似的方法?您是否正在寻找一个简单的分组依据 编辑: 如果要限制内容ID,我建议存在:
以下是将生成所需结果的代码段:
Select
tb2.user_id,count(tb2.content_id)
From table1 tb1
Inner join table2 tb2
on tb1.content_id=tb2.content_id
group by tb2.user_id
假设您希望按t1的内容进行过滤,一种可能性是:
create table t1 (content_id int);
insert into t1 values (2, 3, 4, 5, 6);
-- note I ommitted 1 so not all values are present
create table t2 (content_id int, user_id int);
insert into t2 (values (1,2), (2,3) , (3,2) , (4,1) , (5,3), (6,2) );
select user_id, count(*) from t2 where exists (select 1 from t1 where content_id=t2.content_id) group by user_id;
-- output:
user_id | count
---------+-------
3 | 2
2 | 2
1 | 1
(3 rows)
-- OR
select user_id, count(*) from (select * from t2 except select 1 as user_id, content_id from t1) AS filtered group by user_id;
-- output:
user_id | count
---------+-------
3 | 2
2 | 2
1 | 1
(3 rows)
但其他答案也已经在这样做了。我希望这是您所寻找的,并对您有所帮助
仅希望得到表2中的结果,其中表中还存在内容id1@Lardog . . . 在您的示例数据中,所有的内容ID都在表1中——如果这些表表示正确的关系,我会这样想。更好地表示为。有关如何创建美观的桌子的一些提示,请参见。petre,这很有用,谢谢。可以按计数排序吗?在你们的输出中,它们是按计数排序的,但我认为这是数据的巧合。我的查询输出未按计数排序。是。只需添加一个orderby2或orderbycount或显式命名输出列:选择user\u id,count*作为\u stuff的number\u。。。。按物品的数量订购,效果很好。还有一个复杂问题-user\u id-我想从表3中提取用户名,其中包含user\u id和user\u name,然后我的结果将是user\u name和count,而不是user\u id和count。我可以在这个查询中包含它吗?这是一个用户id上的直接连接。不确定如何或在何处添加它。工作查询:选择用户\u id,从t2计算\u数量,其中存在从t1选择1,其中内容\u id=t2。内容\u id按用户\u id分组按\u数量排序\u数量-我尝试过:选择用户\u id,计算为t2中的\u个数,其中存在从t1中选择1,其中content\u id=t2。content\u id内部连接t2上的t3。user\u id=t3。user\u id按user\u id分组按\u个数排序但不起作用
Select
tb2.user_id,count(tb2.content_id)
From table1 tb1
Inner join table2 tb2
on tb1.content_id=tb2.content_id
group by tb2.user_id
create table t1 (content_id int);
insert into t1 values (2, 3, 4, 5, 6);
-- note I ommitted 1 so not all values are present
create table t2 (content_id int, user_id int);
insert into t2 (values (1,2), (2,3) , (3,2) , (4,1) , (5,3), (6,2) );
select user_id, count(*) from t2 where exists (select 1 from t1 where content_id=t2.content_id) group by user_id;
-- output:
user_id | count
---------+-------
3 | 2
2 | 2
1 | 1
(3 rows)
-- OR
select user_id, count(*) from (select * from t2 except select 1 as user_id, content_id from t1) AS filtered group by user_id;
-- output:
user_id | count
---------+-------
3 | 2
2 | 2
1 | 1
(3 rows)
select user_id, count(`content_id`) from table2 t2
where exists (select t1.content_id from table1 t1
where t1.content_id=t2.content_id)
group by user_id order BY count(`content_id`) DESC;
+---------+---------------------+
| user_id | count(`content_id`) |
+---------+---------------------+
| 2 | 3 |
| 3 | 2 |
| 1 | 1 |
+---------+---------------------+