mysql LIKE with double percent

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-27 07:38:11

问题


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}%%'

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/50863249/mysql-like-with-double-percent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!