在SQL中使用“ WHERE二进制”?

可以在WHERE子句之后使用binary关键字,以将值与完全区分大小写的匹配进行比较。

以下是一个例子-

情况1-不区分大小写的匹配

查询如下-

mysql> select 'joHN'='JOHN' as Result;

以下是输出-

+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

在上面的示例输出中,当我们知道joHN和JOHN是两个不同的词时,结果为true。这不是区分大小写的匹配。

情况2-如果要区分大小写,请使用二进制关键字。

查询如下-

mysql> select binary 'joHN'='JOHN' as Result;

以下是输出-

+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

现在让我们看看另一个查询-

mysql> select binary 'JOHN'='JOHN' as Result;

以下是输出-

+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

注意-每当创建表时,都可以使用Binary关键字,借助binary关键字使列区分大小写。

为了理解上述概念,让我们创建一个表。创建表的查询如下-

mysql> create table binaryKeywordDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(10) binary,
   -> PRIMARY KEY(Id)
   -> );

使用INSERT命令在表中插入一些记录。查询如下-

mysql> insert into binaryKeywordDemo(Name) values('bOB');
mysql> insert into binaryKeywordDemo(Name) values('bob');
mysql> insert into binaryKeywordDemo(Name) values('BOB');
mysql> insert into binaryKeywordDemo(Name) values('Bob');
mysql> insert into binaryKeywordDemo(Name) values('bOb');
mysql> insert into binaryKeywordDemo(Name) values('boB');

使用select语句显示表中的所有记录。查询如下-

mysql> select *from binaryKeywordDemo;

以下是输出-

+----+------+
| Id | Name |
+----+------+
|  1 | bOB  |
|  2 | bob  |
|  3 | BOB  |
|  4 | Bob  |
|  5 | bOb  |
|  6 | boB  |
+----+------+
6 rows in set (0.00 sec)

以下是完全匹配的查询,例如区分大小写-

mysql> select *from binaryKeywordDemo where Name='Bob';

这是输出-

+----+------+
| Id | Name |
+----+------+
|  4 | Bob  |
+----+------+
1 row in set (0.00 sec)
猜你喜欢