MySQL中利用正则表达式查询指定开头与指定结尾的字符串

版权声明:本文为博主原创文章,转载请附上博文链接。 https://blog.csdn.net/qq_41080850/article/details/88068302

建表regexp_example:

create table regexp_example(
prod_name varchar(10) not null default '',
prod_price smallint unsigned not null default 0
)engine myisam character set utf8;


# regexp_example表结构说明:
# prod_name表示产品名称,prod_price表示产品价格

向regexp_example表中插入数据:

insert into regexp_example values
('HTCN12',1999),
('HTCN250',2899),
('HTCNXP',4999),
('ASHTCN09',999)
('HTCN85',1799);

查询场景:从regexp_example表中查询出所有名称形如"HTCNxx"的产品的信息,"HTCNxx"中结尾处的两个x均指的是在0-9之间的任意一个数字。

实现上述查询任务的SQL语句如下:

select * from regexp_example where prod_name REGEXP '^HTCN.{0}[0-9]{2}$';

查询结果如下图所示:

猜你喜欢

转载自blog.csdn.net/qq_41080850/article/details/88068302
今日推荐