mysql其中字符串以数字结尾

标签 mysql

我的表列包含如下值:

id | item
-------------
1  | aaaa11a112
2  | aa1112aa2a
3  | aa11aa1a11
4  | aaa2a222aa

我只想选择项目值以数字结尾的行。 有这样的吗?

select * from table where item like '%number'

最佳答案

您可以使用REGEXP 和字符类

select * from table where item REGEXP '[[:digit:]]$'

DEMO

解释:

[[:digit:]] >> Match digit characters
$           >> Match at the end of the string

在括号表达式(使用 [ 和 ] 编写)中,[:character_class:] 表示匹配属于该类的所有字符的字符类。


旁注:

REGEXP 一起使用的其他有用的 mysql 字符类,取自 documentation :

Character Class Name    Meaning
alnum                   Alphanumeric characters
alpha                   Alphabetic characters
blank                   Whitespace characters
cntrl                   Control characters
digit                   Digit characters
graph                   Graphic characters
lower                   Lowercase alphabetic characters
print                   Graphic or space characters
punct                   Punctuation characters
space                   Space, tab, newline, and carriage return
upper                   Uppercase alphabetic characters
xdigit                  Hexadecimal digit characters

关于mysql其中字符串以数字结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304308/

相关文章:

php - PHP MySQL 不支持 IN 吗?

php - Mysql: 'order by Rand() Limit 4 ' 还是不是?

mysql - 如何搜索不在同一个表中的列?

MySQL If else 构造语法

MYSQL查询中的循环变量

mysql - 如何使用准备好的语句使用数据数组更新多列?

javascript - android 登录页面无法获取 php 响应

javascript - 如何克服NodeJS堆内存错误?

php - 为每个不同的行选择计数(mysql 和 php)

php - 用户注册后使用循环插入语句