获取相关标签的计数

2022-01-18 00:00:00 tags sql-server

我最近问了这个问题以获取 Stack Overflow 中的相关标签:要获取的查询是什么?相关标签"就像堆栈溢出一样

I recently asked this question to get related tags like in Stack Overflow: what is the query to get "related tags" like in stack overflow

我想在 Stack Overflow 中显示计数,所以对于每个相关标签,第一个标签的问题中其他标签的计数是多少?

I would like to show the count like in Stack Overflow, so with each related tag, what is the count of this other tag on questions with the first tag?

因为这些查询都有不同的内容.我不认为这是可能的,但显然 Stack Overflow 必须这样做(除非他们在多个查询中这样做.)

Because these queries all have distinct in them. I don't think it is possible but obviously Stack Overflow must be doing this (unless they are doing this in multiple queries.)

有什么方法可以在同一个查询中获取所有相关标签的计数,还是必须在单独的查询中完成?

Is there any way to get count of related tags all in the same query or must it be done in separate queries?

推荐答案

可以搜索相关标签的总数:

You could search for the total count of related tags:

SELECT t2.tagname, count(distinct tb2.linkid) as RelatedLinkCount
FROM TAGS t2
JOIN TAGS_BRIDGE tb2 on t2.tagid = tb2.tagid
JOIN TAGS_BRIDGE tb1 on tb2.linkid = tb1.linkid
JOIN TAGS t ON t.id = tb1.tagid
WHERE t.tagname = 'tag3'
GROUP BY t2.tagname

从标签名称 (t) 开始,查找该标签的链接 (tb1),然后查找共享链接的标签 (tb2).它会查找相关标签的名称(t2)并完成 :) 甚至不需要加入 LINKS 表.

Starting with the tag name (t), it looks for links for that tag (tb1), then it looks for tags that share a link (tb2). It looks up the name for the related tags (t2) and it's done :) No need to even join the LINKS table.

相关文章