SQL - 查找所有相关标签?

发布于 2024-10-23 19:04:38 字数 1328 浏览 6 评论 0原文

我有一个如下所示的关系标签表。所有三个 node_id (1,2,3) 都存在关系 tag_id“1”。

CREATE TABLE IF NOT EXISTS `relation` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `tag_id` int(10) unsigned NOT NULL,
  `node_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ;

INSERT INTO `relation` (`id`, `tag_id`, `node_id`) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 4, 1),
(5, 1, 2),
(6, 3, 2),
(7, 4, 2),
(8, 5, 2),
(9, 1, 3),
(10, 3, 3),
(11, 5, 3),
(12, 6, 3);

使用tag_id“1”作为起点,如何获取按出现次数排序的所有标签的列表?例如,tag_id“3”存在于所有三个node_id记录中,而tag_id 4仅存在两次(node_id 1和2)。

结果应如下所示:

tag_id  count
3       3
4       2
5       2
6       1
2       1

更新: 抱歉,我解释得不够好。当我说“tag_id“1”作为起点”时,我的意思是如果我搜索与 tag_id 的关系 - 我如何按出现次数对它们进行排序?

我想找到与按计数排序的 tag_id 相关的所有 tag_id 。这将使我能够看到与 tag_id 1 一起使用的所有 tag_id 数字。继续上面的示例 - tag_id "3" 应该是带有 tag_id 1 的 node_ids 最常用的 tag_id。

更新 2: 我认为我在此处找到了答案

SELECT tag_id, COUNT(*) as count FROM relation
WHERE tag_id != 1 AND node_id IN
(
    SELECT node_id FROM relation WHERE tag_id = 1
)
GROUP BY tag_id ORDER BY count DESC LIMIT 0,10

此方法比 LEFT JOIN 更好吗?我能做些什么来提高它的速度吗?

I have a relation tag table like below. Relation tag_id "1" exists for all three node_id (1,2,3).

CREATE TABLE IF NOT EXISTS `relation` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `tag_id` int(10) unsigned NOT NULL,
  `node_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ;

INSERT INTO `relation` (`id`, `tag_id`, `node_id`) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 4, 1),
(5, 1, 2),
(6, 3, 2),
(7, 4, 2),
(8, 5, 2),
(9, 1, 3),
(10, 3, 3),
(11, 5, 3),
(12, 6, 3);

Using tag_id "1" as the starting point, how can I get a list of all tags ordered by number of occurrence? For example, the tag_id "3" exists in all three node_id records - while the tag_id 4 only exists twice (node_id 1 & 2).

The results should look like this:

tag_id  count
3       3
4       2
5       2
6       1
2       1

Update: Sorry, I didn't explain it good enough. When I said "tag_id "1" as the starting point", I meant if I searched for relations to tag_id - how could I order them by occurrence?

I want to find all tag_ids related to tag_id one ordered by count. This will allow me to see all the tag_id numbers used along with tag_id 1. Continuing with the example above - tag_id "3" should be the most used tag_id for node_ids with tag_id 1.

Update 2: I think I found an answer here.

SELECT tag_id, COUNT(*) as count FROM relation
WHERE tag_id != 1 AND node_id IN
(
    SELECT node_id FROM relation WHERE tag_id = 1
)
GROUP BY tag_id ORDER BY count DESC LIMIT 0,10

Is this method better than a LEFT JOIN? Is there anything I can do to improve it's speed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-10-30 19:04:38
select tag_id, Count(*) as TagCount
from relation
where tag_id <> 1
group by tag_id
order by Count(*) desc
select tag_id, Count(*) as TagCount
from relation
where tag_id <> 1
group by tag_id
order by Count(*) desc
与之呼应 2024-10-30 19:04:38

这行得通吗?

SELECT tag_id, COUNT(*)
FROM relation
GROUP BY tag_id
ORDER BY COUNT(*) desc

will this work?

SELECT tag_id, COUNT(*)
FROM relation
GROUP BY tag_id
ORDER BY COUNT(*) desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文