Ordering items with matching tags by number of tags that match(按匹配的标签数量对具有匹配标签的项目进行排序)
问题描述
我正在尝试弄清楚如何根据匹配的标签数量对带有匹配标签的商品进行排序.
I'm trying to figure out how to order items with matching tags by the number of tags that match.
假设您有三个 MySQL 表:
Let's say you have three MySQL tables:
标签(tag_id, title)
文章(article_id, some_text)
articles_tags(tag_id, article_id)
现在假设您有四篇文章,其中:
Now let's say you have four articles where:
article_id = 1
有标签幽默"、搞笑"和搞笑".
article_id = 1
has tags "humor," "funny," and "hilarious."
article_id = 2
有标签有趣"、愚蠢"和愚蠢".
article_id = 2
has tags "funny," "silly," and "goofy."
article_id = 3
有标签有趣"、愚蠢"和愚蠢".
article_id = 3
has tags "funny," "silly," and "goofy."
article_id = 4
带有完全严重"的标签.
article_id = 4
has the tag "completely serious."
您需要通过至少一个匹配的标签找到与article_id = 2
相关的所有文章,并按照匹配最佳的顺序返回结果.换句话说,article_id = 3
应该先出现,article_id = 1
第二,article_id = 4
根本不应该出现.
You need to find all articles related to article_id = 2
by at least one matching tag, and return the results in order of the best matches. In other words, article_id = 3
should come first, with article_id = 1
second, and article_id = 4
should not show up at all.
这是在 SQL 查询中可行还是单独使用,还是更适合 Sphinx 之类的东西?如果是前者,应该做什么样的查询,应该为最高性能的结果创建什么样的索引?如果是后者,请展开.
Is this something that's doable in SQL queries or alone, or is this better suited for something like Sphinx? If the former, what kind of query should be done, and what sort of indexes should be created for the most performant results? If the latter, please do expand.
推荐答案
试试这样的:
select article_id, count(tag_id) as common_tag_count
from articles_tags
group by tag_id
where tag_id in (
select tag_id from articles_tags where article_id = 2
) and article_id != 2
order by common_tag_count desc;
MySQL 的语法可能需要稍作调整.
Syntax may need a little tweaking for MySQL.
或者这个真正有效的:;-)
or this one that actually works: ;-)
SELECT at1.article_id, Count(at1.tag_id) AS common_tag_count
FROM articles_tags AS at1 INNER JOIN articles_tags AS at2 ON at1.tag_id = at2.tag_id
WHERE at2.article_id = 2
GROUP BY at1.article_id
HAVING at1.article_id != 2
ORDER BY Count(at1.tag_id) DESC;
这篇关于按匹配的标签数量对具有匹配标签的项目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:按匹配的标签数量对具有匹配标签的项目进行排序
基础教程推荐
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01