Home > Software design >  LIKE Command in SQL
LIKE Command in SQL

Time:06-25

I tried to write a query with Like command to search for special Name , but i need to get the value from User and i need to read the value but my sql code has problem with "LIKE" section , how can i fix it ?

sql = '''SELECT Name FROM newtest WHERE Name LIKE = %s'''
val = (n)
myobj = mycurs.execute(sql , val)

what's the problem ?

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= %s' at line 1

this is the error

CodePudding user response:

Remove the "=" sign. The LIKE command doesn't use that - see https://www.w3schools.com/sql/sql_ref_like.asp for an example.

CodePudding user response:

If you need to search for non-exact matches (i.e. the name is anywhere inside the column) you can change the query as:

SELECT Name FROM newtest WHERE Name LIKE concat('%', %s, '%')
  • Related