Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用变量的MYSQL视图_Mysql_Variables_Views - Fatal编程技术网

不使用变量的MYSQL视图

不使用变量的MYSQL视图,mysql,variables,views,Mysql,Variables,Views,我想将此查询转换为视图。我需要消除变量的使用。SQL代码如下所示: SET @runningTotal = 0; SELECT feescollected.DatePaid, feescollected.TermPaidFor, fe

我想将此查询转换为视图。我需要消除变量的使用。SQL代码如下所示:

SET @runningTotal = 0;
SELECT
    feescollected.DatePaid,  feescollected.TermPaidFor,                                                                                                                            
    feescollected.FeesPaid,
 @runningTotal := @runningTotal + feescollected.FeesPaid AS runningTotal
FROM  feescollected;
预期产出:

DatePaid    TermPaidFor     FeesPaid    runningTotal 
----------------------------------------------------    
2014-02-06  I               150000      150000
2014-03-24  I               70000       220000
2014-04-08  I               80000       300000
像这样

SELECT
    feescollected.DatePaid,  feescollected.TermPaidFor,                                                                                                                            
    feescollected.FeesPaid,
 (feescollected.FeesPaid + 0) AS runningTotal
FROM  feescollected;

使用子查询根据日期计算总计

SELECT
    a.DatePaid,
    a.TermPaidFor,                                                                                                                            
    a.FeesPaid,
(select  sum(b.FeesPaid) from feescollected b where a.DatePaid >= b.DatePaid) AS runningTotal
FROM  feescollected a

谢谢。虽然一旦我添加了更多的数据和列,结果就会出错。我想我需要学习子查询如何执行。