SQL通配符

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

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

实例

SELECT * FROM Websites
WHERE name LIKE '_oogle';

执行输出结果:

 
 
问题再延伸,如果我们数据中有包含下划线的字符,那么该怎么处理呢?
 
--需求,找到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 '%_%';

 

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

 

-- 那么使用右斜杠+下划线的模式来转译他,可以么?
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

 

-- 使用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 '\';

 

 

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

 

复制代码
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 '!';
复制代码

结果都是

妥了。

 
 
 
 
posted @   一年变大牛  阅读(790)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示