首页 > 解决方案 > Oracle 从值包含下划线的表中选择

问题描述

我有一个查询,我需要从值以 M_ 开头的表中进行选择。问题是下划线是oracle中的通配符。因此,在描述字段中获取以下数据:

M1
M_1
M2
M_2

当我运行以下查询时,将返回所有字段:

select description from table where description like 'M_%'

我尝试使用以下不返回结果的查询进行转义:

select description from table where description like 'M\_%'

我正在寻找的结果只是以 M_ 开头的描述,因此预期的结果是:

M_1
M_2

提前感谢您的帮助

标签: sqloraclesql-like

解决方案


您需要明确指定转义字符:

select description from table where description like 'M\_%' escape '\'

这意味着您可以使用替代字符代替反斜杠。例如:

select description from table where description like 'M$_%' escape '$'

推荐阅读