找出varchar在MySQL中是否包含百分号?
要在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) -> );
使用insert命令插入一些带有%符号的记录。查询如下-
mysql> insert into FindPercentInVarcharDemo(Words) values('This is a My%SQL Program'); mysql> insert into FindPercentInVarcharDemo(Words) values('Java is an object oriented'); mysql> insert into FindPercentInVarcharDemo(Words) values('C# is also an object%oriented');
使用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)