Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql LIKE with double percent

I just debugging a legacy code and I found a strange part in it. Does anybody has an idea, what does the following mean in the MYSQL string?

full_name LIKE '%%{fullname}%%'
like image 703
szatti1489 Avatar asked Jun 14 '18 17:06

szatti1489


People also ask

What does %s do MySQL?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".

Can I use like in MySQL?

The MySQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.


1 Answers

As in the answer Mihir Dave's comment links to, there's no difference to SQL if you pass %% instead of %. Since a single % matches zero or more characters, then each of the metacharacters in %% would also match zero or more, and ultimately the same string would match one way or another.

But I'd guess your legacy code is pre-Python 2.6 that uses % as a metacharacter in string formatting, and you have to double it like %% to get a single literal % character.

See also:

  • How can I selectively escape percent (%) in Python strings?
  • https://docs.python.org/2.6/library/stdtypes.html#string-formatting-operations
like image 175
Bill Karwin Avatar answered Oct 11 '22 02:10

Bill Karwin