【问题标题】:PHP MySQL search for Title and Multiple Tags OrderPHP MySQL搜索标题和多个标签顺序
【发布时间】:2012-08-24 13:32:25
【问题描述】:

我正在寻找改进我网站搜索功能的最佳方法。

我有两个 MySQL 表“文章”和“标签”

->“文章”中的列

aid (auto increment)
headline
..and a few more

->“标签”中的列(每个标签的新条目)

tid (auto increment)
aid (ID from 'articles')
tag

例如,如果我有一篇标题为 “这是一篇绿色文章” 的文章,并且该文章的标签是 “蓝色”、“黄色” p>

假设还有一篇标题为 “这是另一个测试” 的文章,并带有以下标签 “蓝色”、“黄色”、“粉色” “黑色”

如果访客现在正在搜索 “green pink black” mysql 应该找到文章 “This is a green Article” 以及其他文章,因为它的标签“粉红色”“黑色”

此外,我需要一个按相关性对文章进行排序的函数。 所以在这个例子中,文章“这是另一个测试”应该首先显示,因为如果标签 "pink""black" 和文章 "This is一篇绿色文章“位于第二个位置(标题中只有“绿色”)

在过去的几个小时里,我尝试了一些查询,例如 match..against 和 joins,但这对我来说太复杂了。

有人给我提示吗?

(对不起,我的英语不好)

【问题讨论】:

    标签: php mysql search full-text-search


    【解决方案1】:

    您的查询应如下所示:

    SELECT
        DISTINCT articles.aid
    FROM articles
    INNER JOIN tags
        ON articles.aid = tags.aid
    WHERE tags.tag IN ("green", "pink", "black");
    

    由于两个表有一个共同的属性(即文章 ID),您可以使用 INNER JOIN 来连接两个表,并根据两个表中的其他属性(在本例中为标签名称)过滤结果。

    上述查询将为您提供带有绿色、粉色或黑色标签的文章 ID 列表。

    编辑

    抱歉,我没有看到您对相关性的要求。如果您想了解在每篇文章中找到了多少标签,请将其转换为 GROUPed 查询,并计算找到的标签数量:

    SELECT
        articles.aid,
        articles.headline,
        COUNT(tags.tid)
    FROM articles
    INNER JOIN tags
        ON articles.aid = tags.aid
    WHERE tags.tag IN ("green", "pink", "black")
    GROUP BY articles.aid;
    

    试试这个场景...

    首先,设置数据库:

    mysql> CREATE TABLE articles (aid integer auto_increment, headline varchar(32), key(aid));
    Query OK, 0 rows affected (0.13 sec)
    
    mysql> CREATE TABLE tags (tid integer auto_increment, aid integer, tag VARCHAR(16), key(tid));
    Query OK, 0 rows affected (0.09 sec)
    
    mysql> INSERT INTO articles (headline) values ("This is a green Article"), ("This is another Test");
    Query OK, 2 rows affected (0.05 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * FROM articles;
    +-----+-------------------------+
    | aid | headline                |
    +-----+-------------------------+
    |   1 | This is a green Article |
    |   2 | This is another Test    |
    +-----+-------------------------+
    2 rows in set (0.00 sec)
    
    mysql> INSERT INTO tags (aid, tag) VALUES (1, "blue"), (1, "yellow"), (2, "blue"), (2, "yellow"), (2, "pink"), (2, "black");
    Query OK, 6 rows affected (0.04 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    

    然后运行一些查询:

    mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black") GROUP BY articles.aid;
    +-----+----------------------+-----------------+
    | aid | headline             | COUNT(tags.tid) |
    +-----+----------------------+-----------------+
    |   2 | This is another Test |               2 |
    +-----+----------------------+-----------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black", "yellow") GROUP BY articles.aid;
    +-----+-------------------------+-----------------+
    | aid | headline                | COUNT(tags.tid) |
    +-----+-------------------------+-----------------+
    |   1 | This is a green Article |               1 |
    |   2 | This is another Test    |               3 |
    +-----+-------------------------+-----------------+
    2 rows in set (0.00 sec)
    

    【讨论】:

    • 感谢您的回复!这非常适合标签。但我也想在文章标题中搜索。例如,搜索词是“green”,没有标签“green”,但有一个带有“green”字样的文章标题。 (对不起我的英语不好)
    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2012-12-06
    • 2016-11-05
    • 2012-06-04
    • 1970-01-01
    • 2012-04-23
    • 2020-11-17
    相关资源
    最近更新 更多