为什么在MariaDB 10.1中ifnull()返回DECIMAL而不是BIGINT?

例如:

查询:

select a, ifnull(b, 1) from table;


10.0.22-MariaDB:

ifnull(b, 1)类型是BIGINT

10.1.37-MariaDB:

ifnull(b, 1)类型是DECIMAL

此外,在两个版本中,此查询的返回类型都相同:

select 1; //type is BIGINT


为什么ifnull()BIGINT转换为DECIMAL

最佳答案

我无法重现该问题,请参见示例:

MariaDB [test]> SELECT VERSION();
Field   1:  `VERSION()`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     72
Max_length: 24
Decimals:   31
Flags:      NOT_NULL

+-----------------+
| VERSION()       |
+-----------------+
| 10.1.38-MariaDB |
+-----------------+
1 row in set (0.00 sec)

MariaDB [test]> DROP TABLE IF EXISTS `test`;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> CREATE TABLE IF NOT EXISTS `test` (
    ->   `bigint` BIGINT,
    ->   `decimal` DECIMAL(5, 2)
    -> );
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> INSERT INTO `test`
    ->   (`bigint`, `decimal`)
    -> VALUES
    ->   (NULL, NULL);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> SELECT 1;
Field   1:  `1`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     1
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM

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

MariaDB [test]> SELECT
    ->   IFNULL(`bigint`, 1) `bigint`,
    ->   IFNULL(`decimal`, 1) `decimal`
    -> FROM
    ->   `test`;
Field   1:  `bigint`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     20
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM

Field   2:  `decimal`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       NEWDECIMAL
Collation:  binary (63)
Length:     7
Max_length: 4
Decimals:   2
Flags:      NOT_NULL BINARY NUM

+--------+---------+
| bigint | decimal |
+--------+---------+
|      1 |    1.00 |
+--------+---------+
1 row in set (0.00 sec)

关于mysql - 为什么在MariaDB 10.1+中ifnull()返回DECIMAL而不是BIGINT?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55473888/

10-16 15:46