为什么CAST(1 AS SIGNED INTEGER)在MySQL上返回BIGINT?


8

如果我这样做,CAST(1 AS SIGNED INTEGER)最终总是会得到BIGINT退货,例如:

$ mysql -u root -p --column-type-info
Enter password:

--- Copyright and help message snipped for brevity ---

mysql> select cast(1 as signed integer);
Field   1:  `cast(1 as signed integer)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG            <== LONGLONG i.e. 64 bit integer
Collation:  binary (63)
Length:     1
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM


+---------------------------+
| cast(1 as signed integer) |
+---------------------------+
|                         1 |
+---------------------------+
1 row in set (0.00 sec)

我本来希望该类型的返回类型为LONG(32位整数)。

如果我从具有的表中选择一列,INT我会看到它确实只是一个LONG

mysql> describe contact;

+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| contact_id | int(11) | NO   | PRI | NULL    | auto_increment |

      == remainder of table snipped ==

mysql> select contact_id from contact where contact_id = 20;
Field   1:  `contact_id`
Catalog:    `def`
Database:   `centreon`
Table:      `contact`
Org_table:  `contact`
Type:       LONG                     <== LONG i.e. 32 bit integer
Collation:  binary (63)
Length:     11
Max_length: 2
Decimals:   0
Flags:      NOT_NULL PRI_KEY AUTO_INCREMENT NUM PART_KEY


+------------+
| contact_id |
+------------+
|         20 |
+------------+
1 row in set (0.00 sec)

mysql>

如果将同一列转换为有符号整数,则我再次获得返回的64位整数:

mysql> select CAST(contact_id as signed integer) from contact where contact_id = 20;
Field   1:  `CAST(contact_id as signed integer)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     11
Max_length: 2
Decimals:   0
Flags:      NOT_NULL BINARY NUM


+------------------------------------+
| CAST(contact_id as signed integer) |
+------------------------------------+
|                                 20 |
+------------------------------------+
1 row in set (0.00 sec)

这里有一个类似的报道问题:

http://bugs.mysql.com/bug.php?id=64084

但是遗憾的是,OP并没有得到直接的答案。

这是CAST()功能中的错误还是设计上的错误?


纵观文档流延()/()转换,仅提及了64位整数dev.mysql.com/doc/refman/5.7/en/...
Philᵀᴹ

@Phil-我确实读了一遍又一遍。为什么SIGNED [INTEGER]在该部分中说明结果的类型可以是以下值之一:。是SIGNED INTEGER在上下文CAST事实上不是32位整数?
凯夫2014年

我正在阅读“ MySQL支持带符号和无符号64位值的算术运算。如果您使用数字运算符(例如+或-),并且其中一个操作数是无符号整数,则默认情况下,结果是无符号的(请参阅第5章。 12.6.1,“算术运算符”。您可以通过使用SIGNED或UNSIGNED强制转换运算符将值分别强制转换为有符号或无符号64位整数来覆盖此内容。” 部分
Philᵀᴹ

@Phil-是的,我也读过,实际上它的行为符合预期,即SELECT 1+1结果为BIGINT。但它仍然没有解释为什么离开CAST()的行为违背了文档(据我所知),并产生一个BIGINT即使问到投SIGNED INTEGERUNSIGNED INTEGER上一个标量值。
凯夫2014年

我在这里找到的最佳解决方案:将BIGINT UNSIGNED转换为INT

Answers:


0

如您在https://www.w3schools.com/sql/func_mysql_cast.asp上看到的

SIGNED将值转换为SIGNED(带符号的64位整数)

UNSIGNED将值转换为UNSIGNED(无符号的64位整数)

并且从官方的MySQL文档中获取:https : //dev.mysql.com/doc/refman/5.7/en/cast-functions.html

MySQL支持带符号和无符号64位值的算术运算。对于其中一个操作数是无符号整数的数字运算符(例如+或-),默认情况下结果是无符号的(请参见第12.6.1节“算术运算符”)。若要重写此方法,请使用SIGNED或UNSIGNED强制转换运算符将值分别强制转换为有符号或无符号64位整数。

因此,当您使用SIGNED或UNSIGNED数据类型时,看起来CAST输出实际上将是64位整数。


-1

在MySQL中将int存储为32位。

MySQL支持带符号和无符号64位值的算术运算。对于其中一个操作数是无符号整数的数字运算符(例如+或-),默认情况下结果是无符号的。要覆盖此内容,请使用SIGNED或UNSIGNED强制转换运算符将值分别强制转换为有符号或无符号64位整数。

这就是为什么int会显示long int:32 bit

对于带符号的int,它显示long long int或BIG int:64 int

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.