为什么 BINARY 关键字与 MySQL REGEXP 运算符一起使用?
mysqlmysqli database更新于 2025/5/26 6:04:17
使用 BINARY 关键字强制 REGEXP 将字符串匹配为二进制字符串。我们将在这里看到区别。
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.46 sec)
使用 insert 命令在表中插入一些记录。这里有不同情况的名称 −
mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('JOHN');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('john');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('JOhn');
Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
这将产生以下输出 −
+------+ | Name | +------+ | John | | JOHN | | john | | JOhn | +------+ 4 rows in set (0.00 sec)
以下查询用于了解 REGEXP 和 REGEXP 运算符与 BINARY 之间的区别 −
mysql> select Name REGEXP 'JOHN' AS ResultWithOutBinary, Name REGEXP BINARY 'JOHN' AS ResultWithBinary from DemoTable;
输出
这将产生以下输出 −
+---------------------+------------------+ | ResultWithOutBinary | ResultWithBinary | +---------------------+------------------+ | 1 | 0 | | 1 | 1 | | 1 | 0 | | 1 | 0 | +---------------------+------------------+ 4 rows in set (0.04 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

