获取表记录中以数字结尾的字符串
为此,你需要使用 REGEXP。语法如下 −
select *from yourTableName where yourColumnName REGEXP '[[:digit:]]$';
为了理解以上语法,我们创建一个表格。创建表格的查询如下 −
mysql> create table StringEndsWithNumber -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserId varchar(20), -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令在表格中插入一些记录。查询如下 −
mysql> insert into StringEndsWithNumber(UserId,UserName) values('123User','John'); Query OK, 1 row affected (0.18 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('User456','Larry'); Query OK, 1 row affected (0.19 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('User789','John'); Query OK, 1 row affected (0.12 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('0981User','Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('User999','Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('User1290','David'); Query OK, 1 row affected (0.37 sec) mysql> insert into StringEndsWithNumber(UserId,UserName) values('User456','James'); Query OK, 1 row affected (0.21 sec)
使用 select 语句显示表格中的所有记录。查询如下 −
mysql> select *from StringEndsWithNumber;
输出
+----+----------+----------+ | Id | UserId | UserName | +----+----------+----------+ | 1 | 123User | John | | 2 | User456 | Larry | | 3 | User789 | John | | 4 | 0981User | Carol | | 5 | User999 | Bob | | 6 | User1290 | David | | 7 | User456 | James | +----+----------+----------+ 7 rows in set (0.00 sec)
以下是字符串以数字结尾的查询 −
mysql> select *from StringEndsWithNumber where UserId REGEXP '[[:digit:]]$';
输出
+----+----------+----------+ | Id | UserId | UserName | +----+----------+----------+ | 2 | User456 | Larry | | 3 | User789 | John | | 5 | User999 | Bob | | 6 | User1290 | David | | 7 | User456 | James | +----+----------+----------+ 5 rows in set (0.11 sec)
广告