如何在 MySQL 中使用正则表达式查找大写字母?
mysqlmysqli database
您可以使用 REGEXP BINARY 来实现此目的
select *from yourTableName where yourColumnName REGEXP BINARY '[A-Z]{2}';
让我们先创建一个表
mysql> create table FindCapitalLettrsDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentFirstName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录。 查询语句如下 −
mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('JOHN'); Query OK, 1 row affected (0.24 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('John'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from FindCapitalLettrsDemo;
以下是输出 −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1 | JOHN | | 2 | Carol | | 3 | bob | | 4 | carol | | 5 | John | +-----------+------------------+ 5 rows in set (0.00 sec)
这是在 MySQL 中查找大写字母的查询
mysql> select *from FindCapitalLettrsDemo -> where StudentFirstName REGEXP BINARY '[A-Z]{2}';
以下是输出 −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1 | JOHN | +-----------+------------------+ 1 row in set (0.14 sec)