MySQL REGEXP 用于开头的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3712685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL REGEXP for numbers that begin with
提问by pistolshrimp
I need to write a query using MYSQL REGEXP that will find me rows that have a certain column begin with 11 or 12 or etc. I know I can use LIKE or LEFT(####,2) but would like to use the REGEXP option. My data is stored as 110001, 122122, 130013a and etc.
我需要使用 MYSQL REGEXP 编写一个查询,它会找到某个列以 11 或 12 等开头的行。我知道我可以使用 LIKE 或 LEFT(####,2) 但想使用 REGEXP选项。我的数据存储为 110001、122122、130013a 等。
EDIT 1:
编辑 1:
To make it more clear, I would like to express
为了更清楚,我想表达
SELECT * FROM table WHERE column LIKE '11%' or column LIKE '12%' or column LIKE '30%'"
with REGEXP
使用正则表达式
Thanks!
谢谢!
回答by Mark Byers
To match rows that start with any two digits you can use:
要匹配以任意两位数字开头的行,您可以使用:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^[0-9][0-9]';
If you only want to allow rows starting with 11
, 12
or 30
then you could use this:
如果你只想让行开始11
,12
还是30
那么你可以这样做:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^(11|12|30)';
This will not be able to use an index on the column so it may be slower than using LIKE.
这将无法在列上使用索引,因此它可能比使用 LIKE 慢。