How to search something in mysql by searching a part of it

60 views Asked by At

Let's say we have this query:

query = "SELECT surname FROM set_payment WHERE surname = %s"
mycursor.execute(query,("Smith",))

If we have the surname 'Smith' for example in our table it should print Smith. Is there a way to search for something by searching a part of it? For example, if a search for Sm to print all surnames that contain the letters 'Sm'?

2

There are 2 answers

0
Snap On BEST ANSWER

you can use the LIKE operator using %. example: WHERE surname LIKE '%sm%'

0
Rahmatullah Marestiyal On

Yes of course it is possible.

You can use "where" with "like" in MySQL query.

For your search you can write the query like this:

WHERE surname LIKE '%sm%';

this searches all names that contains 'sm'.