mysql - 累计和的 where 子句如何工作?

标签 mysql sql

所以,我有一个表调用顺序,由两个结果组成,结果由一个值组成。

当我执行这个查询时:

SELECT @running_total := @running_total + orders.myVal AS cumulative_sum
From orders JOIN
     (SELECT @running_total := 0) r

它返回这个结果:

_____________________
|id | cumulative_sum |
|___|________________|
| 1 |             1  |
|___|________________|
| 2 |             3  |
|___|________________|  

但是,当我添加 where 子句时 WHERE @running_total <= 0.5

这是

SELECT @running_total := @running_total + orders.myVal AS cumulative_sum
From orders JOIN (SELECT @running_total := 0) 
WHERE @running_total <= 0.5

为什么返回相同的结果?

据我所知,它不应该返回任何结果,因为两个结果都不包含小于 0.5 的值

谢谢

最佳答案

试试这个: 我想,当您使用 @running_total <= 0.5 检查条件时此时此变量中没有分配任何值,因此它始终为 true 并返回值。请查询@running_total <= 0.2也返回 true 并通过 > 检查你也会明白你在做什么

SELECT * FROM (@running_total := @running_total + orders.myVal AS cumulative_sum 
From orders 
JOIN (SELECT @running_total := 0) r) t WHERE @running_total <= 0.5

关于mysql - 累计和的 where 子句如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47921204/

相关文章:

mysql - 从两个表中选择,但仅选择其中一列中的最大值

mysql - RethinkDb 查询两个日期列之间的当前日期时间

mysql - 如何以 CSV 格式输出 MySQL 查询结果?

mysql - mySQL 数据库位于 WordPress 站点的目录树中的什么位置?

php - 注销后用户仍然可以访问 protected 页面

sql server if block errors when running a stored proc inside - 给出语法错误

sql - 需要将值添加到现有的行和列中

mysql - 聚合具有可能空值的列

php - 制作 'wish list' 功能的最佳方法

php - DataTables 导出 MySQL 表中的所有记录