Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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 SQL中的递归查询_Mysql - Fatal编程技术网

Mysql SQL中的递归查询

Mysql SQL中的递归查询,mysql,Mysql,我有两张桌子 组织机构 反式 // money // date // mid // 1000 2015-10-1 1111 201 2015-10-1 2222 201 2015-10-1 3333 1001 2015-10-1 4444 2000 2015-10-2 1111 201 2015-

我有两张桌子

组织机构

反式

//  money  //       date   //  mid  //
     1000      2015-10-1      1111
      201      2015-10-1      2222
      201      2015-10-1      3333
     1001      2015-10-1      4444 
     2000      2015-10-2      1111
      201      2015-10-2      2222
      201      2015-10-2      3333
     1001      2015-10-2      4444 
我用mid连接两个表 我想得到像这样的输出

//  org_name  //  sum_money  //    date  //
       apple           1201   2015-10-1
       apple           2201   2015-10-2  
      google           1202   2015-10-1
      google           1202   2015-10-2   
     unicooo            201   2015-10-1
     unicooo            201   2015-10-2   
        uber           1001   2015-10-1
        uber           1001   2015-10-2    
比如说

//  org_name  //  sum_money  //    date  //
       apple           1201   2015-10-1
苹果在10-1中得到了1000个,unicooo的父id是苹果的id,unicooo在10-1中得到了201个,所以苹果在10-1中得到了1201个。即使苹果和它的“孩子”在10-1中得到了零,我仍然想显示它,并将sum\u money设置为零

我试过了

select sum(a.money) from trans a right join 
(select b.mid from org b where b.parend_id = '1') as c
on a.mid = c.mid group by a.date

幸运的是,我得到了我想要的“apple”,但我想选择所有列,但不知道如何处理。也欢迎使用两个或更多sql查询。

您想要什么请解释更多@aisonmysql不支持递归。。。你应该先研究一下,有什么办法可以让它工作吗?@rahauts通过它的“组织id”和“子代”获得所有的“钱”。在当前的结构中,你需要通过递归执行多个查询来实现这一点。如果可以更改表结构,则可以使用嵌套集模型进行更改。
select sum(a.money) from trans a right join 
(select b.mid from org b where b.parend_id = '1') as c
on a.mid = c.mid group by a.date