如何在 MySQL 中搜索"ñ"并避免包含"n"的记录?
mysqlmysqli database
如果您在搜索"ñ"时不想要所有包含"n"的记录,请使用以下语法 −
select *from yourTableName where yourColumnName LIKE '%ñ%' COLLATE utf8_spanish_ci;
让我们首先创建一个表。以下是查询 −
mysql> create table NotIncludenDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20) -> ); Query OK, 0 rows affected (1.07 sec)
下面是使用 insert 命令在表中插入一些记录的查询 −
mysql> insert into NotIncludenDemo(ClientName) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into NotIncludenDemo(ClientName) values('Johñ'); Query OK, 1 row affected (0.14 sec) mysql> insert into NotIncludenDemo(ClientName) values('Johnñy'); Query OK, 1 row affected (0.18 sec)
以下是使用 select 语句显示表中的所有记录的查询 −
mysql> select *from NotIncludenDemo;
这将产生以下输出 −
+----+------------+ | Id | ClientName | +----+------------+ | 1 | John | | 2 | Johñ | | 3 | Johnñy | +----+------------+ 3 rows in set (0.00 sec)
以下是搜索“ñ”的查询 −
mysql> select *from NotIncludenDemo where ClientName LIKE '%ñ%' COLLATE utf8_spanish_ci;
这将产生以下输出 −
+----+------------+ | Id | ClientName | +----+------------+ | 2 | Johñ | | 3 | Johnñy | +----+------------+ 2 rows in set (0.60 sec)