6

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}%%'
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
szatti1489
  • 352
  • 2
  • 5
  • 16

2 Answers2

5

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:

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Forget it to mention, the environment is Yii1/PHP. Thanks. – szatti1489 Jun 14 '18 at 18:07
  • I haven't used Yii. Is it possible it's using `%{fullname}%` as a token to replace with some content, and then the extra `%` characters on either side are the actual SQL wildcards? – Bill Karwin Jun 14 '18 at 18:11
1

I doubt that it is a legacy issue. I have used double percent in python 3.7 as well, though admittedly on an older mysql version, with double percent having the only function of escaping the second %. I came to this by Python3.8, MySQL5.7: passing active wildcards to LIKE query

questionto42
  • 7,175
  • 4
  • 57
  • 90