3

我有下表。

条目表描述

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

我继续使用全文。你怎么 ?

4

5 回答 5

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}
于 2012-07-26T17:09:21.210 回答
1

Do you mean is to show the attributes the columns entry . title and tag . name by joining entry . user_id into tag . relation_id? If that's what you want, you could check the SQL queries below:

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

Now if you're using query string to search a value on entry . title and tag . name attributes, try to check this code:

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

Example, when a searcher go to domain.com/?q=test the system will then look if there's a matched text for test on entry . title and tag . name and if ever founds some, the row*(s)* of entry . title and tag . name that included that word will be a result.

Now if you want to show all the attributes that included the value of the query string ?q= then just SELECT *. Check this out:

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

Or include all there TableName . AttributeFields manually, such this:

$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. don't forget to rename the "tags" table into "tag".

于 2013-04-07T08:41:24.957 回答
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%')
于 2012-07-26T17:01:01.490 回答
1

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

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

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

于 2013-04-11T23:48:18.263 回答
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}')
于 2013-04-12T20:31:01.130 回答