且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL中的正则表达式以检测一位或多位数字

更新时间:2023-02-10 22:52:58

使用 SQL FIDDLE DEMO >

输出

|         NAME |
|--------------|
| 129387 store |

I have the following query:

SELECT * 
FROM  `shop` 
WHERE  `name` LIKE  '%[0-9]+ store%'

I wanted to match strings that says '129387 store', but the above regex doesn't work. Why is that?

Use REGEXP operator instead of LIKE operator

Try this:

SELECT '129387 store' REGEXP '^[0-9]* store$';

SELECT * FROM shop WHERE `name` REGEXP '^[0-9]+ store$';

Check the SQL FIDDLE DEMO

OUTPUT

|         NAME |
|--------------|
| 129387 store |