如何转义 MySQL 列中字符串左侧和中间的“%”字符?
我们先创建一个表 -
mysql> create table DemoTable629 (StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,StudentSubject text); Query OK, 0 rows affected (0.77 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable629(StudentSubject) values('MySQL%'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable629(StudentSubject) values('Spring%Hibernate'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable629(StudentSubject) values('%Java'); Query OK, 1 row affected (0.21 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable629;
这将产生以下输出 -
+-----------+------------------+ | StudentId | StudentSubject | +-----------+------------------+ | 1 | MySQL% | | 2 | Spring%Hibernate | | 3 | %Java | +-----------+------------------+ 3 rows in set (0.00 sec)
以下是转义“%”字符的查询 -
mysql> select *from DemoTable629 where StudentSubject like '%\%';
这将产生以下输出 -
+-----------+----------------+ | StudentId | StudentSubject | +-----------+----------------+ | 1 | MySQL% | +-----------+----------------+ 1 row in set (0.00 sec)
广告