简体   繁体   English

ajax搜索如何检查某个短语是否包含搜索到的单词

[英]ajax search how to check if certain phrase contain the searched words

so i am doing a AJAX search bar. 所以我正在做一个AJAX搜索栏。 For now i only know how to search for exact words. 现在我只知道如何搜索确切的单词。 Example for my table 我桌子的例子

ID|Skillset|Description|
1|game|i love game
2|photography|i love to take photo
3|game|game is my forte

For now i have this query to search for exact word in the database 现在我有这个查询来搜索数据库中的确切单词

PHP PHP

$sql = "SELECT userdatafiles.UserID,Name,Link,Title FROM userdatafiles JOIN users ON userdatafiles.UserID = users.UserID WHERE Skillsets = '$searchBar' GROUP BY UserID ORDER BY RAND()";

So if the user typed game, it will display all skillsets that matches game. 因此,如果用户键入游戏,它将显示与游戏匹配的所有技能组。

What i want to improve is that, instead of just searching into the Skill set column, i want to search for both Skillset and Description column. 我想改进的是,我不想只搜索技能集列,而是想搜索技巧集和描述列。 Instead of searching for exact matching words, i wan users to be able to type something like 'gam' and it will search in both Skillset & Description as long as there are words that contain 'gam' it will display out, example gam is a part of 'game', or a part of 'i love game'. 我没有搜索完全匹配的单词,而是让用户能够键入类似'gam'的内容,只要有包含'gam'的单词,它就会在Skillset和Description中搜索,例如gam是一个'游戏'的一部分,或'我爱游戏'的一部分。 Any ideas how i can do that? 任何想法我怎么能这样做?

You should use LIKE to compare and CONCAT to select your field search : 您应该使用LIKE进行比较和CONCAT来选择您的字段搜索:

$sql = "SELECT userdatafiles.UserID, Name, Link, Title 
FROM userdatafiles ud JOIN users u ON userdatafiles.UserID = users.UserID 
WHERE concat(ud.Skillsets, ud.description) LIKE '%$searchBar%' 
GROUP BY UserID ORDER BY RAND()";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM