为什么在 MariaDB 10.1+ 中使用 ifnull() return DECIMAL 而不是 BIGINT?

Why ifnull() return DECIMAL instead of BIGINT in MariaDB 10.1+?

为什么在 MariaDB 10.1 中使用 ifnull() return 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

此外,在这两个版本中,此查询的 return 类型是相同的:

select 1; //type is BIGINT

为什么 ifnull()BIGINT 转换为 DECIMAL

默认 return 类型将由传递给函数的两种类型值的比较产生,returned 类型有一些顺序:整数在 bigint 之前

TYPE (X,Y) = IFNULL(type X , type Y). 

在你的情况下,decimal 比 bigin 更通用。

我想关于如何处理默认 return 类型,Mysql 的两个版本都有一些变化。

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

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)