简体   繁体   中英

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

MySQL SELECT query string matching These do not match my requirement.

Hi, I am using PHP with mysql. 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'. 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? 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' "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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