0

I'm new to database and querying in MySql. I have a table in my database with id, fistname, middlename and lastname. In my webapp, I have a search box so that I can search my database using the firstname, middlename and lastname and it will display the result in my web app. Do I need to use SELECT statement to do this and what condition should I query?

Please advise.

Many thanks.

1 Answer 1

2
select id, firstname, middlename, lastname where firstname like '%searchterm%' or middlename like '%searchterm%' or lastname like '%searchterm%'

this will give you a very broad result, as it matches on the search term anywhere within any of the name fields.

you can also use 'searchterm%' to match ones starting with the searchteam, and 'searchterm' for an exact match

Sign up to request clarification or add additional context in comments.

3 Comments

if you want only 1 row returned, them put limit 1 on the end of the query
Thanks. Do I need a special encoding of a space character. Because it gives me no result if I include spaces in my query search like John Bonson?
you would need to split that into parts "john" and "bronson" and match on both of those parts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.