如何检查MySQL中的值是否为整数? [英] How do I check to see if a value is an integer in MySQL?

查看:793
本文介绍了如何检查MySQL中的值是否为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在MySQL中有Cast()Convert()函数可以从值创建整数,但是有什么方法可以检查值是否是整数?我正在寻找PHP中的is_int()之类的东西.

I see that within MySQL there are Cast() and Convert() functions to create integers from values, but is there any way to check to see if a value is an integer? Something like is_int() in PHP is what I am looking for.

推荐答案

我假设您要检查字符串值.一种不错的方法是REGEXP运算符,将字符串与正则表达式匹配.只需

I'll assume you want to check a string value. One nice way is the REGEXP operator, matching the string to a regular expression. Simply do

select field from table where field REGEXP '^-?[0-9]+$';

这相当快.如果您的字段是数字,则只需测试

this is reasonably fast. If your field is numeric, just test for

ceil(field) = field

相反.

这篇关于如何检查MySQL中的值是否为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆