mysql search title, description and multi rows tag

僤鯓⒐⒋嵵緔 提交于 2020-01-02 07:35:16

问题


i have following tables.

the entry table describe

+-------------+-------------------+------+-----+---------+----------------+
| 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    |                |
+-------------+-------------------+------+-----+---------+----------------+

the tags table describe

+-------------+---------------------+------+-----+---------+----------------+
| 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    |                |
+-------------+---------------------+------+-----+---------+----------------+

i want search in entry.title and tag.name. How can the performance for this query.

i continue usign fulltext. How do you ?


回答1:


First off, you need to make sure both the title and name fields are indexed (not able to see that with the information you sent. I assume that tag.relation_id relates to entry.id. SO you could search like:

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:


I'm not sure how the tags relate to the entries, so I'm going to assume the relation_id is the entry 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:


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".




回答4:


So you are looking for a system to search title and then narrow the result using tags. Is this what you are looking for?

To achieve this first display the results for title search and then have tags as the links something like this very page "mysql search title, description and multi rows tag" is your title and in the results and in the result display tags as buttons " mysql search full-text-search " When clicked on tags show specific results.

If you could give more information or a scenario where & what you are trying to achieve, would help us in getting you a better solution.




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


来源:https://stackoverflow.com/questions/11674232/mysql-search-title-description-and-multi-rows-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!