Search keyword(s) that matches data in MySQL

允我心安 提交于 2019-12-25 01:22:18

问题


I am doing a tidyup for a library system and fixing messy book titles. I would like to write an SQL query or PHP code that searches keyword(s) that matches data in MySQL table in a sentence.

[tbl_keywords]

id | keyword             | title
====================================================================
 1 | Harry Potter        | Harry Potter
 2 | Philosopher's Stone | [Harry Potter] Harry Potter and the Philosopher's Stone
 3 | Chamber of Secrets  | [Harry Potter] Harry Potter and the Chamber of Secrets
 4 | Dr. Seuss           | Dr. Seuss
 5 | Green Eggs and Ham  | [Dr. Seuss] Green Eggs and Ham
 6 | The Cat in the Hat  | [Dr. Seuss] The Cat in the Hat

For example,

"Harry Potter(1)" => matches 1
"[Harry Potter] Harry Potter and the Philosopher's Stone(1)" => matches 1 and 2
"(HARRY POTTER2) THE CHAMBER OF SECRETS" => matches 1 and 3
"Dr. Seuss - Green Back Book" => matches 4
"Green eggs and ham" => matches 5
"the cat in the hat(Dr. Seuss)" => matches 4 and 6

Also is this possible (easy to be implemented)? If it's too much to do, I just add variable values into the table..

"HarryPotter" => matches 1
"Dr.Seuss" => matches 4
"Dr Seuss" => matches 4

Any help or ideas on how to do this would be appreciated. Thanks in advance.


回答1:


Use LIKE in your SQL where condition.

Example:

$input_search //your search input
$sql1 = 'Select * from `tbl_keywords` where keyword ="'.$input_search.'"';
//...the rest of code
$sql2 = 'Select * from `tbl_keywords` where keyword LIKE "%'.$input_search.'%" OR title LIKE "%'.$input_search.'%"';
//... the rest of code
//here is IF condition to get the desire result from the sql result you got



回答2:


Jst wrote like this

$element_to_search='//get here the input to search';
$sql2 = 'Select * from `tbl_keywords` where keyword LIKE "%'.$element_to_search.'%" OR title LIKE "%'.$element_to_search.'%"';

In your case always put the key_element which is to be searched will be put between two "%HERE%" because

"HERE%" will results whose key starts with and

"%HERE" will results whose key ends with,

but if you put "%HERE%" it will results all the elements which will contains "key_element" as substring. Thanks




回答3:


Instead of SQL, PHP stristr is used to look for each keywords in array.

search in text , and select keywords by php

Then, look up the IDs in a MySQL table to get other information/fields; title, category etc.

$keywords = array(NULL, 'Harry Potter', 'Philosopher\'s Stone', 'Chamber of Secrets');
$input_title = "[Harry Potter] Harry Potter and the Philosopher\'s Stone(1)"

$keyword_found = array();
foreach ($keywords as $key => $val) {
    if (stristr($input_title, $val)) $keyword_found[] = $key;
}

if ($keyword_found) {
    foreach ($keyword_found as $val) {
        $sql = "SELECT * FROM `tbl_keywords` WHERE `id` = '" . $val . "'";
        ...
    }
}

It's not tidy and there must be better ways, but.. at least it works! Thank you again for people who try to help me :)



来源:https://stackoverflow.com/questions/12275215/search-keywords-that-matches-data-in-mysql

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