0

I am trying to build a search query where suppose if we type a word stem, it should show words starting with stem and whatever follows after that. Right now, my code looks like this:

$sql = "SELECT * FROM `faculty`
    WHERE (CONCAT(`firstName`, ' ', `lastName`,`expertise`,`affiliation`)
    LIKE '%$textSearch%') ORDER BY lastname asc";

If I search stem in the search bar, it will show unneccessary words like 'System'.

giusti
  • 3,156
  • 3
  • 29
  • 44

1 Answers1

0

If you’re looking for a search across multiple columns, but each word must begin with the search term you may have to make it a multiple LIKE statement?

$sql = "SELECT * FROM `faculty` WHERE 
`firstName` LIKE ‘$textSearch%’ OR `lastName` LIKE ‘$textSearch%’ OR `expertise` LIKE ‘$textSearch%’ OR `affiliation` LIKE ‘$textSearch%’ 
 ORDER BY lastname asc";
  • Hi Luke, Thank you for your response. This is working to some extent. However, If I have a expertise column looks like "Hey, I am engineer". It will only work if I type "Hey". If I type something that is in the middle like engineer, it won't work. – Amun Kharel Oct 06 '18 at 00:22
  • Hi Amun, I've found another link on SO which shows a similar issue to what you are having. [https://stackoverflow.com/questions/656951/search-for-whole-word-match-in-mysql] – Luke Corbett Oct 06 '18 at 05:13