SQL通配符

SQL通配符通常我们只是用%作为通配符,用来表示任意个字符。但sql中的通配符还有下划线_,用来标识任意一个字符实例SELECT*FROMWebsitesWHEREnameLIKE&#

大家好,又见面了,我是你们的朋友全栈君。

通常我们只是用 %  作为通配符,用来表示任意个字符。

但sql中的通配符还有 下划线 _ ,用来标识任意一个字符

实例

SELECT * FROM Websites

WHERE name LIKE ‘_oogle’;

执行输出结果:

<span role="heading" aria-level="2">SQL通配符
 
 
问题再延伸,如果我们数据中有包含下划线的字符,那么该怎么处理呢?
 
--需求,找到name中包含下划线的内容,即 把前两条筛出来
with x as
(select '_haha' as name from dual  union all
select '\_2haha'  as name from dual  union all
select '2haha' as name from dual
)
select * from x where name like '%_%';

<span role="heading" aria-level="2">SQL通配符

 

 不对,这里的下划线别理解成了通配符

 

-- 那么使用右斜杠+下划线的模式来转译他,可以么?
with x as
(select '_haha' as name from dual  union all
select '\_2haha'  as name from dual  union all
select '2haha' as name from dual
)
select * from x where name like '%\_%';

 

<span role="heading" aria-level="2">SQL通配符

 

 

 依然不对,这里斜杠并没有起到转译的作用。

查了下,自己以前不认识这个关键字。ESCAPE

 

-- 使用ESCAPE 关键字来表示【这个字符后边的东西,该被识别成普通字符。】?
with x as
(select '_haha' as name from dual  union all
select '\_2haha'  as name from dual  union all
select '2haha' as name from dual
)
select * from x where name like '%\_%' ESCAPE '\';

 

<span role="heading" aria-level="2">SQL通配符

 

 结果对了。那么换个别的字符呢?

 

with x as
(select '_haha' as name from dual  union all
select '\_2haha'  as name from dual  union all
select '2haha' as name from dual
)
select * from x where name like '%?_%' ESCAPE '?';
---

with x as
(select '_haha' as name from dual  union all
select '\_2haha'  as name from dual  union all
select '2haha' as name from dual
)
select * from x where name like '%!_%' ESCAPE '!';

结果都是

<span role="heading" aria-level="2">SQL通配符

妥了。

 
 
 
 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155710.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号