如何使用 MySQL LIKE 查询在包含百分号 (%) 的列值中进行搜索?
要在包含 % 的列值中进行搜索,语法如下所示:-
select * from yourTableName where yourColumnName LIKE '\%%';
首先创建一张表:-
mysql> create table DemoTable1497 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
使用 insert 在表中插入一些记录:-
mysql> insert into DemoTable1497 values('%JohnSmith');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1497 values('DavidMiller');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1497 values('CarolTaylor%');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1497 values('%DavidMiller');
Query OK, 1 row affected (0.12 sec)使用 select 显示表中的所有记录:-
mysql> select * from DemoTable1497;
这将生成以下输出:-
+--------------+ | Name | +--------------+ | %JohnSmith | | DavidMiller | | CarolTaylor% | | %DavidMiller | +--------------+ 4 rows in set (0.00 sec)
以下是对列值包含 % 的查询:-
mysql> select * from DemoTable1497 -> where Name LIKE '\%%';
这将生成以下输出:-
+--------------+ | Name | +--------------+ | %JohnSmith | | %DavidMiller | +--------------+ 2 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP