如果我这样做,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)
这里有一个类似的报道问题:
但是遗憾的是,OP并没有得到直接的答案。
这是CAST()
功能中的错误还是设计上的错误?
SIGNED [INTEGER]
在该部分中说明结果的类型可以是以下值之一:。是SIGNED INTEGER
在上下文CAST
事实上不是32位整数?
SELECT 1+1
结果为BIGINT
。但它仍然没有解释为什么离开CAST()
的行为违背了文档(据我所知),并产生一个BIGINT
即使问到投SIGNED INTEGER
或UNSIGNED INTEGER
上一个标量值。