mysql搜索标题,描述和多行标记

时间:2012-07-26 16:55:19

标签: mysql search full-text-search

我有以下表格。

条目表描述

+-------------+-------------------+------+-----+---------+----------------+
| 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中搜索。该查询的性能如何。

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

5 个答案:

答案 0 :(得分:1)

首先,您需要确保标题和名称字段都已编入索引(无法通过您发送的信息查看。我认为tag.relation_id与entry.id有关。所以您可以搜索如下:< / p>

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%')

答案 1 :(得分:1)

我不确定标签与条目的关系,所以我假设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}

答案 2 :(得分:1)

您的意思是通过将entry . title加入tag . name来显示列entry . user_idtag . relation_id的属性吗?如果这是你想要的,你可以检查下面的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上是否存在测试的匹配文字和tag . name如果有的话,那么包含该词的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 . AttributeField,例如:

$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 . '%"');

P.S。不要忘记将“标签”表重命名为“tag”

答案 3 :(得分:1)

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

要实现此目的,首先显示标题搜索的结果,然后将标记作为链接 像这样的页面 “mysql搜索标题,描述和多行标记”是您的标题,在结果和结果中显示标签为按钮“mysql search full-text-search”当点击标签显示特定结果时。

如果您可以提供更多信息或情景,其中&amp;你想要实现的目标,将有助于我们为你提供更好的解决方案。

答案 4 :(得分:1)

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}')