【问题标题】:mysql search title, description and multi rows tagmysql 搜索标题、描述和多行标签
【发布时间】:2012-07-25 07:56:31
【问题描述】:

我有以下表格。

入口表描述

+-------------+-------------------+------+-----+---------+----------------+
| Field       | Type              | Null | Key | Default | Extra          |
+-------------+-------------------+------+-----+---------+----------------+
| id          | int(11) unsigned  | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255)      | YES  |     | NULL    |                |
| slug        | varchar(255)      | YES  |     | NULL    |                |
| description | text              | YES  |     | NULL    |                |
| user_id     | int(10) unsigned  | NO   |     | NULL    |                |
| unsafe      | enum('0','1')     | NO   |     | NULL    |                |
| copyright   | enum('0','1')     | NO   |     | 0       |                |
| status      | enum('0','1','2') | NO   |     | 0       |                |
| date_add    | datetime          | NO   |     | NULL    |                |
+-------------+-------------------+------+-----+---------+----------------+

标签表描述

+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| relation_id | int(10) unsigned    | NO   |     | NULL    |                |
| name        | varchar(255)        | NO   |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

我想在 entry.title 和 tag.name 中搜索。这个查询的性能如何。

我继续使用全文。你好吗?

【问题讨论】:

  • 您可以使用 union: 1-st query with fulltext 作为标题;第二次查询连接的标签并使用tag.name IN('tag1','tag2'..)。假设您想要标签的完整单词匹配。
  • vatev 感谢您的回答。您可以编写任何示例查询来回答?

标签: mysql search full-text-search


【解决方案1】:

首先,您需要确保标题和名称字段都已编入索引(无法通过您发送的信息看到这一点。我假设 tag.relation_id 与 entry.id 相关。所以您可以搜索:

SELECT * FROM entry as e JOIN tags as t on e.id = t.relation_id WHERE e.title LIKE('%YOURSEARCH%') OR t.name LIKE('%YOURSEARCH%')

【讨论】:

  • 看起来不太好。如果匹配我想要搜索结果的标签。或匹配标题我想向下搜索结果
【解决方案2】:

我不确定标签与条目之间的关系,所以我假设relation_id 是条目ID。

SELECT 
    e.*
FROM entry e
WHERE MATCH(title) AGAINST ('{search string}')
{maybe a limit and/or order here}
UNION
SELECT
    DISTINCT
    e.*
FROM entry e
JOIN tags t ON t.relation_id = e.id
WHERE t.name IN ('word1','word2',...)#{search string} split by whitespace
{maybe a limit and/or order here}

【讨论】:

    【解决方案3】:

    您的意思是通过将entry . user_id 加入tag . relation_id 来显示entry . titletag . name 列的属性?如果这是你想要的,你可以检查下面的 SQL 查询:

    SELECT entry . title , tag . name
    FROM entry
    INNER JOIN tag ON entry . user_id = tag . relation_id
    ORDER BY entry . title
    

    现在,如果您使用查询字符串搜索 entry . titletag . name 属性的值,请尝试检查以下代码:

    $q = $_GET["q"];
    
    $result = mysqli_query($con, 'SELECT entry . title , tag . name
    FROM entry
    INNER JOIN tag ON entry . user_id = tag . relation_id
    WHERE entry . title LIKE "%' . $q . '%"
    OR tag . name LIKE "%' . $q . '%"');
    

    例如,当搜索者访问 domain.com/?q=test 时,系统将查看 entry . title 和 @ 上的 test 是否有匹配的文本987654330@ 并且如果找到了一些,entry . titletag . name 的行*(s)* 将包含该单词。

    现在,如果您想显示包含查询字符串 ?q= 值的所有属性,那么只需 SELECT *。看看这个:

    $q = $_GET["q"];
    
    $result = mysqli_query($con, 'SELECT *
    FROM entry
    INNER JOIN tag ON entry . user_id = tag . relation_id
    WHERE entry . title LIKE "%' . $q . '%"
    OR tag . name LIKE "%' . $q . '%"');
    

    或者手动包含所有TableName . AttributeFields,例如:

    $q = $_GET["q"];
    
    $result = mysqli_query($con, 'SELECT entry . id , entry . title , entry . slug , entry . description , entry . user_id , entry . copyright , entry . status , tag . id , tag . relation_id , tag . name
    FROM entry
    INNER JOIN tag ON entry . user_id = tag . relation_id
    WHERE entry . title LIKE "%' . $q . '%"
    OR tag . name LIKE "%' . $q . '%"');
    

    附:不要忘记将 "tags" 表重命名为 "tag"

    【讨论】:

      【解决方案4】:

      因此,您正在寻找一个系统来搜索标题,然后使用标签缩小结果范围。这是你要找的吗?

      要实现这一点,首先显示标题搜索的结果,然后将标签作为链接 像这样的页面 “mysql search title, description and multi rows tag”是你的标题,在结果和结果中显示标签为按钮“mysql search full-text-search”当点击标签时显示具体结果。

      如果您能提供更多信息或您想要实现的目标和场景,将有助于我们为您提供更好的解决方案。

      【讨论】:

      • 并且不要忘记在标题和标签中添加索引键,以提高性能。
      【解决方案5】:
      SELECT entries.id, entries.title
      FROM entries
      INNER JOIN tags ON entries.id = tags.relation_id
      WHERE tags.name IN ("tag1", "tag2", "tag3") AND MATCH(entries.title, entries.description) AGAINST ('{text}')
      

      【讨论】:

        猜你喜欢
        • 2016-11-05
        • 2022-06-14
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 2013-05-28
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多