我在MySQL中看到有Cast()
和Convert()
函数从值创建整数,但有没有办法检查一个值是否为整数?像PHP中的is_int()
这样的东西就是我要找的东西。
答案 0 :(得分:189)
我假设您要检查字符串值。一个很好的方法是REGEXP运算符,将字符串与正则表达式匹配。只需做
select field from table where field REGEXP '^-?[0-9]+$';
这是相当快的。如果您的字段是数字,只需测试
ceil(field) = field
代替。
答案 1 :(得分:13)
将其与正则表达式匹配。
c.f。 http://forums.mysql.com/read.php?60,1907,38488#msg-38488引用如下:
Re:MySQL中的IsNumeric()子句?
发布者:kevinclark()
日期:2005年8月8日下午01:01
我同意。这是我为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]+)$';
这允许在开头有一个可选的加号/减号,一个可选的小数点和其余的数字。
答案 2 :(得分:10)
假设我们的字段包含字母数字字段,其中包含
等条目a41q
1458
xwe8
1475
asde
9582
.
.
.
.
.
qe84
并且您希望此db列中的最高数值(在本例中为9582)然后此查询将帮助您
SELECT Max(column_name) from table_name where column_name REGEXP '^[0-9]+$'
答案 3 :(得分:8)
这是一个简单的解决方案 假设数据类型是varchar
select * from calender where year > 0
如果年份为数字,则返回true,否则为false
答案 4 :(得分:6)
这也有效:
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
答案 5 :(得分:4)
要检查Mysql中的值是否为Int,我们可以使用以下查询。 此查询将为行提供Int值
SELECT col1 FROM table WHERE concat('',col * 1) = col;
答案 6 :(得分:3)
怎么样:
WHERE table.field = "0" or CAST(table.field as SIGNED) != 0
测试数字和证据:
WHERE table.field != "0" and CAST(table.field as SIGNED) = 0
答案 7 :(得分:2)
我已尝试使用上面列出的正则表达式,但它们不适用于以下内容:
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会将字符串解释为有效数字,而实际上并非如此。
答案 8 :(得分:2)
我能想到的最好的变量是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. so LENGTH(Cast('A'为UNSIGNED))= LENGTH(0)将 评估为1 = 1 =&gt; 1
True 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 |
请参阅demo
答案 9 :(得分:0)
这适用于VARCHAR,它以数字开头或不是。
WHERE concat('',fieldname * 1) != fieldname
当你到达更大的NNNNE + - 数字时,可能会受到限制
答案 10 :(得分:0)
对我来说唯一有用的是:
CREATE FUNCTION IsNumeric (SIN VARCHAR(1024)) RETURNS TINYINT
RETURN SIN REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
在234jk456
或12 inches
的情况下,来自kevinclark的所有其他返回无用的内容