要检查字符串是否包含数字,可以使用regexp,即正则表达式。语法如下-
SELECT *FROM yourTableName where yourColumnName REGEXP ‘[0-9]’;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table StringContainsNumber -> ( -> Id int not null auto_increment, -> Words text, -> primary key(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into StringContainsNumber(Words) values('He12345llo'); mysql> insert into StringContainsNumber(Words) values('MySQL is not a programming 4language'); mysql> insert into StringContainsNumber(Words) values('Java is an object oriented'); mysql> insert into StringContainsNumber(Words) values('Java does not support 456 multiple inheritance'); mysql> insert into StringContainsNumber(Words) values('MySQL is a RDBMS 456');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from StringContainsNumber;
以下是输出。
+----+------------------------------------------------+ | Id | Words | +----+------------------------------------------------+ | 1 | He12345llo | | 2 | MySQL is not a programming 4language | | 3 | Java is an object oriented | | 4 | Java does not support 456 multiple inheritance | | 5 | MySQL is a RDBMS 456 | +----+------------------------------------------------+ 5 rows in set (0.00 sec)
这是使用REGEXP查找具有数字的字符串的查询-
mysql> select *from StringContainsNumber where Words regexp '[0-9]';
以下是输出-
+----+------------------------------------------------+ | Id | Words | +----+------------------------------------------------+ | 1 | He12345llo | | 2 | MySQL is not a programming 4language | | 4 | Java does not support 456 multiple inheritance | | 5 | MySQL is a RDBMS 456 | +----+------------------------------------------------+ 4 rows in set (0.11 sec)