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

时间:2019-04-02 11:38:22

标签: mysql sql mariadb ifnull

为什么在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

2 个答案:

答案 0 :(得分:2)

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

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)

答案 1 :(得分:0)

默认返回类型将由传递给函数的两个类型值的比较得出,返回类型有一些顺序:bigint之前的整数

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

在这种情况下,小数比比bigin更通用。

我想关于Mysql的两个版本在处理默认返回类型方面都有一些更改。