Se lo faccio CAST(1 AS SIGNED INTEGER)finisco sempre per ottenere un BIGINTreso, ad esempio:
$ 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)
Mi sarei aspettato che il tipo restituito da quel cast fosse un LONG(intero a 32 bit).
Se seleziono una colonna da una tabella che ha un INTvedo che è davvero solo un 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>
Se eseguo il cast della stessa colonna su un numero intero con segno, ottengo nuovamente un numero intero a 64 bit restituito:
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)
C'è un problema analogo riportato qui:
Ma purtroppo l'OP non ottiene una risposta diretta.
È un bug nella CAST()funzione o è di progettazione?
SIGNED [INTEGER]nella sezione Il tipo per il risultato può essere uno dei seguenti valori: . È un SIGNED INTEGERnel contesto di una CASTnon di fatto un intero a 32 bit?
SELECT 1+1risultati in a BIGINT. Ma non spiega ancora perché CAST()si comporti in contrasto con la documentazione (per come la capisco) e produce BIGINTanche se viene chiesto di eseguire il cast su SIGNED INTEGERo UNSIGNED INTEGERsu un singolo valore scalare.