Answers:
我假设您要检查一个字符串值。一种不错的方法是REGEXP运算符,将字符串与正则表达式匹配。简单地做
select field from table where field REGEXP '^-?[0-9]+$';
这相当快。如果您的字段是数字,则只需测试
ceil(field) = field
代替。
将其与正则表达式匹配。
cf http://forums.mysql.com/read.php?60,1907,38488#msg-38488,如下所示:
Re:MySQL中的IsNumeric()子句?
发表者:kevinclark()
Date:2005年8月8日01:01 PM
我同意。这是我为MySQL 5创建的函数:
CREATE FUNCTION IsNumeric (sIn varchar(1024)) RETURNS tinyint
RETURN sIn REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
这样可以在开头添加一个可选的正/负号,一个可选的小数点和其余数字。
假设我们有一个包含字母数字字段的列,其条目类似于
a41q
1458
xwe8
1475
asde
9582
.
.
.
.
.
qe84
并且您想要此数据库列中的最高数值(在这种情况下为9582),那么此查询将为您提供帮助
SELECT Max(column_name) from table_name where column_name REGEXP '^[0-9]+$'
这是假设数据类型为varchar的简单解决方案
select * from calender where year > 0
如果年份为数字,则返回true,否则返回false
这也适用:
CAST( coulmn_value AS UNSIGNED ) // will return 0 if not numeric string.
例如
SELECT CAST('a123' AS UNSIGNED) // returns 0
SELECT CAST('123' AS UNSIGNED) // returns 123 i.e. > 0
SELECT CAST('12a34' AS UNSIGNED)
,这回报12
?
要检查Mysql中的值是否为Int,我们可以使用以下查询。该查询将为行提供Int值
SELECT col1 FROM table WHERE concat('',col * 1) = col;
我能想到的最好的一个变量是int是与MySQL函数CAST()
和的组合LENGTH()
。
此方法适用于字符串,整数,双精度/浮点型数据类型。
SELECT (LENGTH(CAST(<data> AS UNSIGNED))) = (LENGTH(<data>)) AS is_int
参见演示http://sqlfiddle.com/#!9/ff40cd/44
如果该列具有单个字符值,它将失败。如果列的值为'A',则Cast('A'为UNSIGNED)的值将为0,而LENGTH(0)将为1。因此LENGTH(Cast('A'为UNSIGNED))= LENGTH(0)的值将为1 = 1 => 1
真正的Waqas Malik完全怀疑这件事。补丁是。
SELECT <data>, (LENGTH(CAST(<data> AS UNSIGNED))) = CASE WHEN CAST(<data> AS UNSIGNED) = 0 THEN CAST(<data> AS UNSIGNED) ELSE (LENGTH(<data>)) END AS is_int;
结果
**Query #1**
SELECT 1, (LENGTH(CAST(1 AS UNSIGNED))) = CASE WHEN CAST(1 AS UNSIGNED) = 0 THEN CAST(1 AS UNSIGNED) ELSE (LENGTH(1)) END AS is_int;
| 1 | is_int |
| --- | ------ |
| 1 | 1 |
---
**Query #2**
SELECT 1.1, (LENGTH(CAST(1 AS UNSIGNED))) = CASE WHEN CAST(1.1 AS UNSIGNED) = 0 THEN CAST(1.1 AS UNSIGNED) ELSE (LENGTH(1.1)) END AS is_int;
| 1.1 | is_int |
| --- | ------ |
| 1.1 | 0 |
---
**Query #3**
SELECT "1", (LENGTH(CAST("1" AS UNSIGNED))) = CASE WHEN CAST("1" AS UNSIGNED) = 0 THEN CAST("1" AS UNSIGNED) ELSE (LENGTH("1")) END AS is_int;
| 1 | is_int |
| --- | ------ |
| 1 | 1 |
---
**Query #4**
SELECT "1.1", (LENGTH(CAST("1.1" AS UNSIGNED))) = CASE WHEN CAST("1.1" AS UNSIGNED) = 0 THEN CAST("1.1" AS UNSIGNED) ELSE (LENGTH("1.1")) END AS is_int;
| 1.1 | is_int |
| --- | ------ |
| 1.1 | 0 |
---
**Query #5**
SELECT "1a", (LENGTH(CAST("1.1" AS UNSIGNED))) = CASE WHEN CAST("1a" AS UNSIGNED) = 0 THEN CAST("1a" AS UNSIGNED) ELSE (LENGTH("1a")) END AS is_int;
| 1a | is_int |
| --- | ------ |
| 1a | 0 |
---
**Query #6**
SELECT "1.1a", (LENGTH(CAST("1.1a" AS UNSIGNED))) = CASE WHEN CAST("1.1a" AS UNSIGNED) = 0 THEN CAST("1.1a" AS UNSIGNED) ELSE (LENGTH("1.1a")) END AS is_int;
| 1.1a | is_int |
| ---- | ------ |
| 1.1a | 0 |
---
**Query #7**
SELECT "a1", (LENGTH(CAST("1.1a" AS UNSIGNED))) = CASE WHEN CAST("a1" AS UNSIGNED) = 0 THEN CAST("a1" AS UNSIGNED) ELSE (LENGTH("a1")) END AS is_int;
| a1 | is_int |
| --- | ------ |
| a1 | 0 |
---
**Query #8**
SELECT "a1.1", (LENGTH(CAST("a1.1" AS UNSIGNED))) = CASE WHEN CAST("a1.1" AS UNSIGNED) = 0 THEN CAST("a1.1" AS UNSIGNED) ELSE (LENGTH("a1.1")) END AS is_int;
| a1.1 | is_int |
| ---- | ------ |
| a1.1 | 0 |
---
**Query #9**
SELECT "a", (LENGTH(CAST("a" AS UNSIGNED))) = CASE WHEN CAST("a" AS UNSIGNED) = 0 THEN CAST("a" AS UNSIGNED) ELSE (LENGTH("a")) END AS is_int;
| a | is_int |
| --- | ------ |
| a | 0 |
观看演示
SELECT "a", (LENGTH(CAST("a" AS UNSIGNED))) = CASE WHEN CAST("a" AS UNSIGNED) = 0 THEN CAST("a" AS UNSIGNED) ELSE (LENGTH("a")) END AS is_int;
set @val = '1.'; SELECT @val, LENGTH(CAST(@val AS SIGNED)) = IF(CAST(@val AS SIGNED) = 0, CAST(@val AS SIGNED), LENGTH(@val)) AS is_int;
此重构可以处理所有上述情况,但是即使我的调整也无法处理-1.0或'-1'。再次,超级酷的解决方案。
关于什么:
WHERE table.field = "0" or CAST(table.field as SIGNED) != 0
测试数字和颜色:
WHERE table.field != "0" and CAST(table.field as SIGNED) = 0
我已经尝试使用上面列出的正则表达式,但是它们不适用于以下情况:
SELECT '12 INCHES' REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$' FROM ...
上面的代码将返回1
(TRUE
),表示针对字符串'12 INCHES'对上面的正则表达式进行测试TRUE
。根据上面使用的正则表达式,它看起来像一个数字。在这种情况下,由于12在字符串的开头,因此正则表达式将其解释为数字。
以下内容将返回正确的值(即0
),因为字符串以字符而不是数字开头
SELECT 'TOP 10' REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$' FROM ...
上面的将返回0
(FALSE
),因为字符串的开头是文本而不是数字。
但是,如果要处理包含数字和以数字开头的字母的混合字符串,则不会获得所需的结果。实际上,REGEXP会将字符串解释为有效数字。
FALSE
按预期方式返回,因为正则表达式以$
表示字符串的结尾结尾,因此它仅根据作者的意图检查数字。
这对于以数字开头或不以数字开头的VARCHAR都适用。
WHERE concat('',fieldname * 1) != fieldname
当您使用较大的NNNNE +号时,可能会有限制
set @val = '5'; SELECT @val, concat('', @val * 1) != @val is_int;