如何在 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)

更新日期: 2019 年 7 月 30 日

402 人浏览

开始您的 事业

通过完成课程获得认证

开始
广告
© . All rights reserved.