我使用PHP和mysqli从我的数据库中获取记录。
表引擎= innoDB。
MYSQL v5.5
假设我有这样的记录
1|美丽的瑞士女孩
2|公园里的小白兔
3|天然大海
现在,如果搜索查询是Beautiful,它将返回前2条记录,但问题是,如果我搜索Beautiful anything,它将不返回任何内容,但我希望在这种情况下显示前2条记录,因为它包含单词beautiful。我现在使用SELECT * FROM table WHERE name LIKE '%value%' ORDER BY id ASC作为mysql查询进行搜索。
是否有任何其他查询或方法来实现这一点,或者唯一可能的选择是开发一个自定义算法。
发布于 2014-09-22 19:03:04
这将完成以下工作:
SELECT * FROM table WHERE name LIKE '%Beautiful%' OR name LIKE '%anything%'这是你必须使用的OR。因此,假设您从某处收到一个查询:
// get your query
$query = <...somewhere....>
// explode into words
$words = explode(' ',$query);
// write conditions, pay attention to security!
foreach ($words as $word)
{
$escaped = ESCAPE($word); // depends the db extension you use
$conditions[] = "name LIKE '%$escaped%'";
}
// combine into sql
$sql = "SELECT * FROM table WHERE ".implode(' OR ',$conditions);然后对查询中的所有单词执行OR。
发布于 2014-09-22 19:03:22
为此,我建议您使用Full-Text Searches。它将完全满足您有关使用此http://viralpatel.net/blogs/full-text-search-using-mysql-full-text-search-capabilities/进行全文搜索的更多详细信息的要求
如果您使用的是innodb而不是myisam引擎,请尝试使用此链接
http://mechanics.flite.com/blog/2014/07/08/fine-tuning-mysql-full-text-search-with-innodb/
https://stackoverflow.com/questions/25972476
复制相似问题