如何在MySQL查询中进行正则表达式 [英] How to regex in a MySQL query
本文介绍了如何在MySQL查询中进行正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的任务,我需要搜索一个以字符串字符和其后的一位数字开头的记录.我正在尝试的是这个
I have a simple task where I need to search a record starting with string characters and a single digit after them. What I'm trying is this
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA[d]%')
还有
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA[0-9]%')
但是两个查询总是返回null
记录
But both of the queries always return a null
record
trecord
-------
null
好像我执行以下查询一样
Where as if I execute the following query
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA%')
它返回
trecord
-------
ALA0000
ALA0001
ALA0002
这意味着我有以ALA开头且后跟一个数字的记录,
It means that I have records that starts with ALA and a digit after it,
编辑
我正在使用PHP MySQL和innodb引擎来实现它.
I'm doing it using PHP MySQL and innodb engine to be specific.
推荐答案
I think you can use REGEXP instead of LIKE
SELECT trecord FROM `tbl` WHERE (trecord REGEXP '^ALA[0-9]')
这篇关于如何在MySQL查询中进行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文