了解在一个 varchar 中是否包含百分号?
要在 MySQL 中找出 varchar 是否包含百分号,可以使用 LIKE 运算符。语法如下 -
SELECT * FROM yourTableName WHERE yourColumnName like '%|%%' escape '|';
为了理解以上语法,让我们创建一个表。创建表的查询如下 -
mysql> create table FindPercentInVarcharDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Words varchar(30), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.51 sec)
使用 insert 命令插入包含 % 符号的一些记录。查询如下 -
mysql> insert into FindPercentInVarcharDemo(Words) values('This is a My%SQL Program'); Query OK, 1 row affected (0.19 sec) mysql> insert into FindPercentInVarcharDemo(Words) values('Java is an object oriented'); Query OK, 1 row affected (0.19 sec) mysql> insert into FindPercentInVarcharDemo(Words) values('C# is also an object%oriented'); Query OK, 1 row affected (0.57 sec)
使用 select 语句显示表中的所有记录。查询如下 -
mysql> select *from FindPercentInVarcharDemo;
以下为输出 -
+----+-------------------------------+ | Id | Words | +----+-------------------------------+ | 1 | This is a My%SQL Program | | 2 | Java is an object oriented | | 4 | C# is also an object%oriented | +----+-------------------------------+ 3 rows in set (0.00 sec)
以下是要找出包含 % 符号的 varchar 的查询。查询如下 -
mysql> select *from FindPercentInVarcharDemo where Words like '%|%%' escape '|';
以下仅显示包含 % 符号的值的输出 -
+----+-------------------------------+ | Id | Words | +----+-------------------------------+ | 1 | This is a My%SQL Program | | 4 | C# is also an object%oriented | +----+-------------------------------+ 2 rows in set (0.00 sec)
广告