MySQL 如何使用正则表达式(Regex)找到大写字母
您可以使用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)
阅读更多:MySQL 教程
极客教程