MySQL and LIKE comparison with %

半世苍凉 提交于 2019-12-11 08:58:05

问题


If someone passes a '%' to a field that compares in my sql with su.username LIKE CONCAT('%', email ,'%')) it returns all rows. It ends up looking like su.username LIKE CONCAT('%%%'). Can I get around this in anyway without filtering out the '%'?


回答1:


I'm assuming you mean you want to escape the % so it matches a literal % instead of anything.

In that case, you just need:

... su.username LIKE CONCAT('%',REPLACE(email,'%','\\%'),'%')



回答2:


You need to escape the %, so it literally matches '%'

select * from mytable
where mycol like '%\%%';


来源:https://stackoverflow.com/questions/11059414/mysql-and-like-comparison-with

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