Oracle 专题
专题目录
您的位置:database > Oracle专题 > Oracle Like子句
Oracle Like子句
作者:--    发布时间:2019-11-20

在本教程中,您将学习如何使用oracle like运算符来测试列中的值是否与指定的模式匹配。

oracle like运算符介绍

有时候,想根据指定的模式来查询数据。 例如,您可能希望查找姓氏以st开头或姓氏以er结尾的联系人。在这种情况下,可使用oracle like运算符。

oracle like运算符的语法如下所示:

expresion [not] like pattern [ escape escape_characters ]

在上面的语法中,

  • expression - 该表达式是一个列名称或一个表达式,要针对该模式(pattern)进行测试。
  • pattern - 该模式是在表达式中搜索的字符串。此模式包含以下通配符:
    • (百分号)匹配零个或多个字符的任何字符串。
    • _(下划线)匹配任何单个字符。
  • escape_character - escape_character是出现在通配符前面的字符,用于指定通配符不应被解释为通配符而是常规字符。

escape_character(如果指定)必须是一个字符,并且没有默认值。

如果表达式匹配模式,like运算符返回true。 否则,它返回false

not运算符(如果指定)否定like运算符的结果。

oracle like示例

下面举一些使用oracle like运算符的例子来看看它是如何工作的。

这里将使用示例数据库中的contacts表进行演示:

oracle like

1. %通配符的例子

以下示例使用通配符查找姓氏以st开头的联系人的电话号码:

select
    first_name,
    last_name,
    phone
from
    contacts
where
    last_name like 'st%'
order by
    last_name;

执行上面查询语句,得到以下结果 -

like语句

在这个例子中,使用了这个模式:

'st%'

like运算符匹配任何以“st”开头的字符串,后跟任意数量的字符,例如stokessteinsteele等。

要查找姓氏以字符串“er”结尾的联系人的电话号码,请使用以下语句:

select
    first_name,
    last_name,
    phone
from
    contacts
where
    last_name like '%er'
order by
    last_name;

执行上面查询语句,得到以下结果 -

匹配的模式 -

%er

匹配任何以“er”字符串结尾的字符串。

要执行不区分大小写的匹配,可以使用lower()upper()函数,如下所示:

upper( last_name ) like 'st%'

lower(last_name like 'st%'

例如,以下语句查找名字以ch开头的联系人的电子邮件:

select
    first_name,
    last_name,
    email
from
    contacts
where
    upper( first_name ) like 'ch%'
order by
    first_name;

执行上面查询语句,得到以下结果 -

以下示例使用not like运算符来查找电话号码不以“+1”开头的联系人:

select
  first_name, last_name, phone
from
  contacts
where
  phone not like '+1%'
order by
  first_name;

执行上面查询语句,得到以下结果 -

_ 通配符的例子

以下示例查找名字具有以下模式“je_i”的联系人的电话号码和电子邮件:

select
    first_name,
    last_name,
    email,
    phone
from
    contacts
where
    first_name like 'je_i'
order by 
    first_name;

执行上面查询语句,得到以下结果 -

模式'je_i'匹配任何以'je'开头的字符串,然后是一个字符,最后是'i',例如jerijeni,但不是jenni

3. 混合通配符字符的例子

可以在模式中混合通配符。例如,以下语句查找名字以j开头,后跟两个字符,然后是任意数量字符的联系人。换句话说,它将匹配以je开头并且至少有4个字符的任何姓氏(first_name):

select
    first_name,
    last_name,
    email,
    phone
from
    contacts
where
    first_name like 'je_%';

执行上面查询语句,得到以下结果 -

4. escape子句的例子

escape子句允许查找包含一个或多个通配符的字符串。

例如,表可能包含具有百分比字符的数据,例如折扣值,折旧率。要搜索字符串25%,可以使用escape子句,如下所示:

like '%25!%%' escape '!'

如果不使用escape子句,则oracle将返回字符串为25的任何行。

以下语句创建折扣(discounts)表并插入一些用于测试的示例数据:

create table discounts
  (
    product_id number, 
    discount_message varchar2( 255 ) not null,
    primary key( product_id )
  );

-- 插入3条数据
insert into discounts(product_id, discount_message)
values(1, 'buy 1 and get 25% off on 2nd ');

insert into discounts(product_id, discount_message)
values(2, 'buy 2 and get 50% off on 3rd ');


insert into discounts(product_id, discount_message)
values(3, 'buy 3 get 1 free');

如果不熟悉此脚本中使用的语句,则可以在随后的教程中学习它们。以下语句检索折扣25%的产品:

select
   product_id, discount_message
from
   discounts
where
   discount_message like '%25!%%' escape '!';

执行上面查询语句,得到以下结果 -


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册