Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to search in a MYSQL database for items with keyword-1*keyword-2. keyword-1 will be prefix(surname) and keyword-2(lastname) will be suffix. By giving those two i have to find the full name. Note: Total name including surname,first name and last name are in a single column.

What I have tried:

Method-1: Finding with One keyword. (Now i want to update for two keywords)
SQL
select name from data where model like 'keyword1%'


Method-2: Regular expressions method.
SQL
select name from data where (name REGEXP '^keyword[a-z]*')
Posted
Updated 19-Jan-20 21:22pm

1 solution

Try:
SQL
SELECT [Name] FROM Data WHERE Model LIKE 'Keyword1%Keyword2'

Do note that your regex example won't work at all unless all the names are in all lower case, and there is no separating character between "surname" and "lastname" - for your username, it would match "junglee" but not "jung lee", "jungLEE", or even "jung ".

I'd strongly suggest that if you want to treat name parts as separate items ("surname" and "lastname" for example) then you store them as separate items in separate columns - it makes subsequent processing a lot easier if you speratate them in your presentation software where the string handling facilities are much, much better than SQL can cope with.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900