8

我想使用 LIKE 运算符来匹配列中的可能值。

如果该值以“CU”开头,后跟一个数字(例如“3”),然后是其他任何内容,我想返回它。使用下划线的任何单个字符似乎只有一个通配符,但是我需要确保它是一个数字而不是 az。我试过这些无济于事:

select name from table1 where name like 'CU[0-9]%'
select name from table1 where name like 'CU#%'

优选地,这可能是区分大小写的,即如果cu或Cu或cU那么这将不匹配。

4

4 回答 4

12

You need to use regexp:

select name
from table1
where name regexp binary '^CU[0-9]'

The documentation for regexp is here.

EDIT: binary is required to ensure case-sensitive matching

于 2013-06-04T20:31:40.453 回答
7

The like operator only have the % and _ wildcards in MySQL, but you can use a regular expression with the rlike operator:

select name from table1 where name rlike '^CU[0-9]'
于 2013-06-04T20:32:38.320 回答
1

Have you tried with:

select name from table where name between 'CU0' and 'CU9'
于 2013-06-04T20:31:23.373 回答
1

您可以使用 REGEXP 运算符,请参阅http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

所以你的查询是:

select name from table where name regexp 'CU[0-9].*';
于 2013-06-04T20:33:47.080 回答