简体   繁体   English

SQL查询以匹配字符串中的至少n个字符,即使中间缺少某些字符

[英]SQL query to match atleast n characters in a string even if some characters are missed in the middle

Using SQL to match characters in query string 使用SQL匹配查询字符串中的字符

MySQL SELECT query string matching These do not match my requirement. MySQL SELECT查询字符串匹配这些不符合我的要求。

Hi, I am using PHP with mysql. 嗨,我在MySQL中使用PHP。 I have a 'job search' functionality in my website, where we can search jobs by city. 我的网站上有“职位搜索”功能,可以在这里按城市搜索职位。 For example if we post a job in Hyderabad city, and if a user search for the job by using the same word 'Hyderabad' he gets the results. 例如,如果我们在海得拉巴市发布工作,并且如果用户使用相同的单词“海得拉巴”搜索工作,那么他会得到结果。 But if he searches by typing Hydrabad('e' is missing) he cannot get the results and also same problem with 'Delhi', 'New Delhi', 'NewDelhi'. 但是,如果他通过键入Hydrabad(缺少“ e”)进行搜索,那么他将无法获得结果,并且同样会遇到“ Delhi”,“ New Delhi”,“ NewDelhi”的问题。 If I use wild card operators like, 如果我使用通配符运算符,

"SELECT * FROM jobs_tbl WHERE job_city LIKE '%".$search_city."%' "
"SELECT * FROM jobs_tbl WHERE job_city LIKE '%%".$search_city."%%' "

they wont work for me because I shoud get results though some letters and spaces are missed in the string. 他们不会为我工作,因为尽管字符串中缺少一些字母和空格,但我仍应获得结果。 So, can you please tell me if there is any possibility to fetch a row if atleast 4 or 5 characters are matched with the search string? 因此,如果至少4或5个字符与搜索字符串匹配,您能否告诉我是否有可能获取一行? I searched for such operator but could not find any. 我搜索了这样的运算符,但找不到任何运算符。 Or is there any alternative way? 还是有其他替代方法?

You can put % between each character. 您可以在每个字符之间插入%

$like_city = preg_replace('//', '%', $search_city); // Hyderbad => %H%y%d%e%r%b%a%d%
$sql = "SELECT * FROM jobs_tbl WHERE job_city LIKE '$like_city' "

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

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