您可以使用二进制在MySQL中搜索确切的字符串。语法如下:
SELECT * FROM yourTableName WHERE BINARY yourColumnName = yourStringValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table ExactSearch -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserId varchar(10), -> UserName varchar(20), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into ExactSearch(UserId,UserName) values('USER12','John'); mysql> insert into ExactSearch(UserId,UserName) values('12USER','Carol'); mysql> insert into ExactSearch(UserId,UserName) values('USER123','Bob'); mysql> insert into ExactSearch(UserId,UserName) values('USER231','Sam');
使用select语句显示表中的所有记录:
mysql> select *from ExactSearch;
以下是输出:
+----+---------+----------+ | Id | UserId | UserName | +----+---------+----------+ | 1 | USER12 | John | | 2 | 12USER | Carol | | 3 | USER123 | Bob | | 4 | USER231 | Sam | +----+---------+----------+ 4 rows in set (0.00 sec)
这是在MySQL中搜索确切字符串的查询。我们正在搜索字符串“ USER123”:
mysql> select *from ExactSearch where binary UserId = 'USER123';
以下是输出:
+----+---------+----------+ | Id | UserId | UserName | +----+---------+----------+ | 3 | USER123 | Bob | +----+---------+----------+ 1 row in set (0.00 sec)