如何在 MySQL 中从 varchar 列中仅选择非数字值?
mysqlmysqli database
您需要为此使用 REGEXP。语法如下
SELECT *FROM yourTableName WHERE yourColumnName REGEXP '[a-zA-Z]';
为了理解这个概念,让我们创建一个表。创建表的查询如下
mysql> create table SelectNonNumericValue -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserId varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些记录。 查询语句如下
mysql> insert into SelectNonNumericValue(UserId) values('123John'); Query OK, 1 row affected (0.12 sec) mysql> insert into SelectNonNumericValue(UserId) values('58475Carol98457Taylor24'); Query OK, 1 row affected (0.52 sec) mysql> insert into SelectNonNumericValue(UserId) values('199575Sam124'); Query OK, 1 row affected (0.14 sec) mysql> insert into SelectNonNumericValue(UserId) values('Mike2456'); Query OK, 1 row affected (0.14 sec) mysql> insert into SelectNonNumericValue(UserId) values('1000'); Query OK, 1 row affected (0.12 sec) mysql> insert into SelectNonNumericValue(UserId) values('1001'); Query OK, 1 row affected (0.21 sec) mysql> insert into SelectNonNumericValue(UserId) values('10293Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into SelectNonNumericValue(UserId) values('David'); Query OK, 1 row affected (0.30 sec) mysql> insert into SelectNonNumericValue(UserId) values('2456'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录。查询如下
mysql> select *from SelectNonNumericValue;
以下是输出 −
+----+-------------------------+ | Id | UserId | +----+-------------------------+ | 1 | 123John | | 2 | 58475Carol98457Taylor24 | | 3 | 199575Sam124 | | 4 | Mike2456 | | 5 | 1000 | | 6 | 1001 | | 7 | 10293Bob | | 8 | David | | 9 | 2456 | +----+-------------------------+ 9 rows in set (0.00 sec)
以下是选择非数字值的查询
mysql> select *from SelectNonNumericValue WHERE UserId REGEXP '[a-zA-Z]';
以下是忽略数字值的输出
+----+-------------------------+ | Id | UserId | +----+-------------------------+ | 1 | 123John | | 2 | 58475Carol98457Taylor24 | | 3 | 199575Sam124 | | 4 | Mike2456 | | 7 | 10293Bob | | 8 | David | +----+-------------------------+ 6 rows in set (0.00 sec)