sql like语句中的通配符:百分号、下划线和escape

今天查资料查到7年前自己写的blog,被网上转载了几十次,却没一个把我的原文链接标记出来,鄙视一下。

找了半天才找到原贴:http://huxiao.iteye.com/blog/629030

就像我们平时用的*和?,sql的like语句中,使用%和_来代表任意多个字符和一个字符

# %代表任意多个字符  
select * from user where username like '%huxiao';    
select * from user where username like 'huxiao%';    
select * from user where username like '%huxiao%';  
  
# %代表一个字符  
select * from user where username like '_';    
select * from user where username like '______';    
select * from user where username like 'huxia_';    
select * from user where username like 'h_xiao';  

如果我就真的要查%或者,怎么办呢?使用escape,转义字符后面的%或就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用

select username from gg_user where username like '%xiao/_%' escape '/';  
select username from gg_user where username like '%xiao/%%' escape '/';  
    原文作者:老胡de博客
    原文地址: https://www.jianshu.com/p/6dfeb91d6b51
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞