Mysql 使用通配符选择以特定字符开头的名称无效
我试图使用通配符来选择first_name列中以“b”、“s”或“p”开头的所有名称,方法是使用Mysql 使用通配符选择以特定字符开头的名称无效,mysql,sql,Mysql,Sql,我试图使用通配符来选择first_name列中以“b”、“s”或“p”开头的所有名称,方法是使用 SELECT first_name from actor where first_name like '[bsp]%'; 您还没有指定使用哪个版本的MySQL,这会改变对正则表达式的支持级别。向后兼容版本(回到MySQL~v5.x)使用: 正则表达式模式取代MySQL%通配符 声明应将条件解释为正则表达式 从actor中选择first_name,其中first_name regexp'[bsp].
SELECT first_name
from actor
where first_name like '[bsp]%';
您还没有指定使用哪个版本的MySQL,这会改变对正则表达式的支持级别。向后兼容版本(回到MySQL~v5.x)使用:
%
通配符我认为应该是:
SELECT first_name
from actor
where first_name like 'b%' or first_name like 's%' or first_name like 'p%';
这项工作也包括:
SELECT first_name FROM actor WHERE first_name REGEXP '^b|^s|^p';
在MySQL 4.1和MariaDB 10.3.10上测试,最简单的方法是正则表达式:
where first_name regexp '^[bsp]';
与like
模式不同,正则表达式不需要匹配整个表达式<如果模式在任何地方匹配,则code>regexp返回true。因此,不需要等价的%
(在正则表达式中是*
)
您还可以使用:
where left(first_name, 1) in ('b', 's', 'p')
where left(first_name, 1) in ('b', 's', 'p')