1

Query 1:

"SELECT name FROM mytable WHERE name LIKE '%e%'";

This query will look for all names that contain the letter ‘e’.

Query 2:

"SELECT name FROM mytable WHERE name LIKE '%s'" % search_name;

This query will look for all names that contain match search_name exactly.

What I want to know is if there is a way to query for a partial match like in query 1 while using the placeholder in query 2?

I have tried something like:

"SELECT name FROM mytable WHERE name LIKE %'%s'%" % search_name; <- didn’t work; "SELECT name FROM mytable WHERE name LIKE '%%s%'" % search_name; <- also didn’t work;

Shadow
  • 33,525
  • 10
  • 51
  • 64
Evan
  • 288
  • 1
  • 10
  • 19
  • 2
    I don't think this has got anything to do with MySQL or sql. You must be using some kind of a programming language that can expand the `%s` placeholder. This is a question for that programming language, not for any of the tags you used for this question. The tags added by Vadim are completely irrelevant as well. – Shadow Jun 26 '18 at 10:45
  • Your right sorry, I am running the sql queries in python, which is where the placeholder comes from. – Evan Jun 26 '18 at 10:51

1 Answers1

1

Either use a different way of formatting the string inside your query string like

SELECT name FROM mytable WHERE name LIKE '%" + search_name + "%'"

or f-Strings.

Or you could add more % and escape it with a slash so it gets put into the string normally.

\%%s\%
Bernhard
  • 1,253
  • 8
  • 18