Si je le fais, CAST(1 AS SIGNED INTEGER)
je finis toujours par obtenir un BIGINT
retour, par exemple:
$ 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)
Je m'attendais à ce que le type de retour de cette distribution soit un LONG
(entier 32 bits).
Si je sélectionne une colonne d'une table qui a un INT
je vois que c'est en effet juste 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>
Si je transforme la même colonne en un entier signé, je reçois à nouveau un entier 64 bits:
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)
Il y a un problème similaire signalé ici:
Mais malheureusement, le PO ne reçoit pas de réponse claire.
Est-ce un bug dans la CAST()
fonction ou est-ce par conception?
SIGNED [INTEGER]
dans la section Le type du résultat peut être l' une des valeurs suivantes: . Un SIGNED INTEGER
dans le contexte d'un CAST
pas en fait un entier 32 bits?
SELECT 1+1
résultats dans un BIGINT
. Mais cela n'explique toujours pas pourquoi CAST()
se comporte contrairement à la documentation (si je comprends bien) et produit un BIGINT
même si on lui demande de convertir en SIGNED INTEGER
ou UNSIGNED INTEGER
sur une seule valeur scalaire.