MySQL - SELECT中的同时浮点递增和赋值在5.6之前中断

con*_*cat 15 mysql select mariadb

像查询为什么SELECT (@sum:=(@var:=@sum)+some_table.val)...自动转换(@var:=@sum)到整数MySQL的≤5.5为DECIMALsome_table.val和地板它DOUBLE/FLOAT?更改了哪些功能以允许5.6中的预期行为?


背景与细化:

请考虑下表:

CREATE TABLE t ( 
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    sum_component FLOAT 
);
INSERT INTO t (sum_component) VALUES (0.5), (0.6), (0.4), (0.5);
Run Code Online (Sandbox Code Playgroud)

我正在设计一个累积和查询,它获取id累积和大于某个值的条目.通常,此查询符合条例草案:

SELECT t.id, 
    @cumulative_sum
    FROM t 
    CROSS JOIN (SELECT @cumulative_sum:=0) a 
    WHERE (@cumulative_sum:=@cumulative_sum+t.sum_component) > 1.3 
    ORDER BY id ASC LIMIT 1;
Run Code Online (Sandbox Code Playgroud)

...但我碰巧还需要在所选条目之前存储累积总和以及以后的计算,并且cumulative_sum从该查询中不返回预期结果,重复计算最后一个条目.在这种情况下,我希望此查询设置一个存储value 1.1(0.5 + 0.6)的变量,而不必进行额外的数学运算.

如果我在增量步骤中指定@cumulative_sumto 的旧值@another_variable,我应该能够做到这一点.

SELECT t.id, 
    @cumulative_sum
    FROM t 
    CROSS JOIN (SELECT @cumulative_sum:=0) a 
    WHERE (@cumulative_sum:=(@another_variable:=@cumulative_sum)+t.sum_component) > 1.3
    ORDER BY id ASC LIMIT 1;
Run Code Online (Sandbox Code Playgroud)

在我的两台机器上 - 一台运行MySQL 5.6,另一台运行MariaDB 10.0.7 - 上述查询按预期执行:

MariaDB [a51]>     SELECT  t.id, @cumulative_sum
    FROM  t
    CROSS JOIN  
      ( SELECT  @cumulative_sum:=0) a
    WHERE  (@cumulative_sum:=(@another_variable:=@cumulative_sum)
                             +t.sum_component) > 1.3
    ORDER BY  id ASC
    LIMIT  1;

+----+--------------------+
| id | @cumulative_sum    |
+----+--------------------+
|  3 | 1.5000000298023224 |
+----+--------------------+
1 row in set (0.00 sec)

MariaDB [a51]> SELECT @another_variable;
+-------------------+
| @another_variable |
+-------------------+
| 1.100000023841858 |
+-------------------+
1 row in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

但在MySQL 5.5上,它没有:

mysql>     SELECT  t.id, @cumulative_sum
    FROM  t
    CROSS JOIN  
      ( SELECT  @cumulative_sum:=0) a
    WHERE  (@cumulative_sum:=(@another_variable:=@cumulative_sum)+t.sum_component) > 1.3
    ORDER BY  id ASC
    LIMIT  1;

Empty set (0.18 sec)

mysql> SELECT @another_variable;
+-------------------+
| @another_variable |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

观察查询如何递增,我们可以看到潜在的问题.以下是结果FLOAT sum_component:

mysql>     SELECT  t.id, (@cumulative_sum := (@another_variable:=@cumulative_sum)
                  + t.sum_component) AS cumulative_sum,
        sum_component
    FROM  t
    CROSS JOIN  
      ( SELECT  @cumulative_sum:=0) a
    ORDER BY  id ASC;

+----+----------------+---------------+
| id | cumulative_sum | sum_component |
+----+----------------+---------------+
|  1 |            0.5 |           0.5 |
|  2 |            0.6 |           0.6 |
|  3 |            0.4 |           0.4 |
|  4 |            0.5 |           0.5 |
+---+----------------+----------------+
4 rows in set (0.04 sec)
Run Code Online (Sandbox Code Playgroud)

这是一个DECIMAL sum_component看起来像:

mysql> ALTER TABLE t MODIFY sum_component DECIMAL(4,2);
Query OK, 4 rows affected, 2 warnings (0.16 sec)
Records: 4  Duplicates: 0  Warnings: 2

mysql>     SELECT  t.id, (@cumulative_sum := (@another_variable:=@cumulative_sum) 
                           + t.sum_component) AS cumulative_sum,
        sum_component
    FROM  t
    CROSS JOIN  
      ( SELECT  @cumulative_sum:=0) a
    ORDER BY  id ASC;

+----+----------------+---------------+
| id | cumulative_sum | sum_component |
+----+----------------+---------------+
|  1 |           0.50 |          0.50 |
|  2 |           1.60 |          0.60 |
|  3 |           2.40 |          0.40 |
|  4 |           2.50 |          0.50 |
+----+----------------+---------------+
4 rows in set (0.18 sec)
Run Code Online (Sandbox Code Playgroud)

SQL小提琴

mik*_*n32 2

这是因为MySQL在旧版本中将其视为0整数;您的初始分配@cumulative_sum:=0是将变量设置为整数。将分配更改为@cumulative_sum:=0.0会在 5.5 上产生所需的行为:

SELECT t.id,
    @cumulative_sum
    FROM t
    CROSS JOIN (SELECT @cumulative_sum:=0.0) a
    WHERE (@cumulative_sum:=(@another_variable:=@cumulative_sum)+t.sum_component) > 1.3
    ORDER BY id ASC LIMIT 1;
Run Code Online (Sandbox Code Playgroud)

该手册解决了这个问题,但我找不到 5.5 和 5.6 之间行为变化的提及:

在同一非 SET 语句中为变量赋值并读取该值的另一​​个问题是变量的默认结果类型基于其在语句开头的类型。下面的例子说明了这一点:

mysql> SET @a='test';

mysql> SELECT @a,(@a:=20) FROM tbl_name;

对于此 SELECT 语句,MySQL 向客户端报告第一列是字符串,并将 @a 的所有访问转换为字符串,即使第二行的 @a 设置为数字。SELECT 语句执行后,@a 被视为下一条语句的编号。

为了避免此行为出现问题,请不要在单个语句中为同一变量赋值并读取该变量的值,或者在使用该变量之前将其设置为 0、0.0 或 '' 以定义其类型。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.44-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> CREATE TABLE t ( 
    ->     id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    ->     sum_component FLOAT 
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO t (sum_component) VALUES (0.5), (0.6), (0.4), (0.5);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT t.id,
    ->     @cumulative_sum
    ->     FROM t
    ->     CROSS JOIN (SELECT @cumulative_sum:=0.0) a
    ->     WHERE (@cumulative_sum:=(@another_variable:=@cumulative_sum)+t.sum_component) > 1.3
    ->     ORDER BY id ASC LIMIT 1;
+----+----------------------------------+
| id | @cumulative_sum                  |
+----+----------------------------------+
|  3 | 1.500000029802322400000000000000 |
+----+----------------------------------+
1 row in set (0.00 sec)

mysql> SELECT @another_variable;
+-------------------+
| @another_variable |
+-------------------+
| 1.100000023841858 |
+-------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)