如何通过某些标签获取帖子和评论的用户数?
Posted
技术标签:
【中文标题】如何通过某些标签获取帖子和评论的用户数?【英文标题】:How to get user counts for both posts and comments, by certain tags? 【发布时间】:2018-10-05 14:25:12 【问题描述】:使用此数据浏览器查询:
select b.DisplayName as owner, a.N from
(select OwnerUserId, count(*) as N from Posts where (Tags like '%perl6%' or Tags like '%rakudo%' or (Tags like '%parrot%' and Tags like '%perl%')) group by OwnerUserId) as a,
(select Id, DisplayName from Users) as b
where a.OwneruserId = b.Id order by N desc;
我可以列出所有发布带有特定标签的问题的用户。 但是,我还想列出在带有该标签的帖子中回答或评论过的用户。
我猜它涉及使用Comment
表,但是我不清楚如何合并两个表中的 UserId。
【问题讨论】:
【参考方案1】:参考the SEDE schema:
-
按标签获取问题。
对
Tags
表使用测试比在标签列上使用LIKE
操作更好。前者可以快 20 倍。
使用第 1 步中的问题列表获取答案。
同时使用问题列表和答案列表来获取 cmets。
请注意,SEDE provides wonderful magic columns 与 [User Link]
一样。
总而言之,这是一种方法:
WITH questsByTags AS (
SELECT DISTINCT
q.Id
, q.OwnerUserId
FROM Posts q
INNER JOIN PostTags pt ON q.Id = pt.PostId
INNER JOIN Tags t ON t.Id = pt.TagId
WHERE q.PostTypeId = 1 -- questions
AND (
t.TagName = 'perl6'
OR t.TagName = 'rakudo'
OR (
t.TagName = 'parrot'
AND EXISTS (
SELECT * FROM PostTags pt2
INNER JOIN Tags t2 ON t2.Id = pt2.TagId
WHERE q.Id = pt2.PostId AND t2.TagName = 'perl'
) ) )
),
answersByTags AS (
SELECT a.Id
, a.OwnerUserId
FROM Posts a
INNER JOIN questsByTags qbt ON qbt.Id = a.ParentId
),
commntsByTags AS (
SELECT c.Id
, c.UserId AS [OwnerUserId]
FROM Comments c
INNER JOIN (
SELECT Id FROM questsByTags
UNION ALL SELECT Id FROM answersByTags
) AS allPosts
ON allPosts.Id = c.PostId
),
allUsers AS (
SELECT OwnerUserId FROM questsByTags
UNION SELECT OwnerUserId FROM answersByTags
UNION SELECT OwnerUserId FROM commntsByTags
)
SELECT au.OwnerUserId AS [User Link]
, (SELECT Count (qbt.Id) FROM questsByTags qbt WHERE qbt.OwnerUserId = au.OwnerUserId) AS [Num Qsts]
, (SELECT Count (abt.Id) FROM answersByTags abt WHERE abt.OwnerUserId = au.OwnerUserId) AS [Num Ans]
, (SELECT Count (cbt.Id) FROM commntsByTags cbt WHERE cbt.OwnerUserId = au.OwnerUserId) AS [Num Cmmnts]
FROM allUsers au
WHERE au.OwnerUserId IS NOT NULL
ORDER BY [Num Qsts] DESC, [Num Ans] DESC, [Num Cmmnts] DESC
您可以在this SEDE link.this SEDE link.观看现场直播
【讨论】:
以上是关于如何通过某些标签获取帖子和评论的用户数?的主要内容,如果未能解决你的问题,请参考以下文章
如何获取用户 ID 并以 Angular 6 的形式返回他的帖子及其评论
如何使用 mongodb 和 mongoose 从帖子模型中计算整体帖子活动(喜欢、不喜欢、评论)和用户模型中的参考总和