2016-07-27 62 views
1

我正在开发家app.I检索数据使用查询费用和来自不同表名称收入分别作为开支和收入。 如果我只使用费用,那么它会打印,如果我尝试混合收入价值,它会给我问题。合并表使用日期字段,如果共同,并添加新行,如果日期字段不常见

代码:

$expenses = $expense->dailyExpense(); 
$income = $income->dailyIncome(); 
return response()->json(['data' => ['expenses' => $expenses , 'income' => $income] , 'msg' => 'Daily Expense']); 

收入查询部分是:

public function dailyIncome() 
    { 

     return $this->makeModel() 
      ->select(DB::raw("sum(cost) as income"),"date") 
      ->groupBy('date') 
      ->get(); 
    } 

收入查询部分是:

public function dailyExpense() 
    { 

     return $this->makeModel() 
      ->select(DB::raw("sum(cost) as cost") , "date" , DB::raw("dayname(date) as calendar")) 
      ->groupBy('date') 
      ->get(); 
    } 

客户端部分:

$scope.genereateReport = function() { 

    $scope.choices = ['Daily', 'Monthly', 'Yearly']; 
    $scope.$watch('selection', function (newVal, oldVal) { 
     switch (newVal) { 
      case 'Daily': 

       $scope.url = $scope.base_path + 'dailyExpenses'; 
       $http.get($scope.url).success(function (response) { 
        $scope.expenses = response.data.expenses; 
        $scope.income = response.data.income; 
        $scope.totalExpenses = response.data.totalExpenses; 


       }); 

       $scope.displayedCollection = [].concat($scope.expenses,$scope.income); 
       console.log("collection:" + $scope.displayedCollection); 
       $scope.totalExpenses = [].concat($scope.$totalExpenses); 
       // $scope.income = [].concat($scope.income); 
       // 

       $scope.itemsByPage = 10; 
       break; 
} 
); 
}; 

视图部分:在JSON形式上述查询的

<tr> 
           <th class="table-header-check">S.N</th> 
           <th st-sort="date" class="table-header-repeat line-left minwidth-1">Date</th> 
           <th st-sort="date" class="table-header-repeat line-left minwidth-1">Calandar</th> 
           <th st-sort="cost" class="table-header-repeat line-left minwidth-1"> 
            Expense 
           </th> <th st-sort="cost" class="table-header-repeat line-left minwidth-1"> 
            Income 
           </th> 

          </tr> 


          </thead> 
          <tbody> 

          <tr data-ng-repeat="row in displayedCollection track by $index" > 
           <td><% $index+1 %></td> 
           <td><%row.date%></td> 
           <td><%row.calendar%></td> 
           <td><%row.cost%></td> 

           <td><%income['row.date']%></td> 


          </tr> 
          <tr> 
           <td colspan="4"> 
            Total Expense: <%totalExpenses%> 
           </td> 


          </tr> 

结果是:

enter image description here

View是这样,不显示收入: enter image description here

希望的输出是这样的..

enter image description here] 3

回答

1

您可以使用sql unionjoin来获得预期结果。而不是创建两个单独的功能,你可以创建一个功能,并使用原始查询象下面这样:

select e.date,e.cost,'0' as income 
from test.expenses as e 
where e.date not in (select i.date from test.incomes as i) 
union all 
select e.date, e.cost as cost, i.income as income 
from test.expenses as e 
inner join test.incomes as i on e.date = i.date 
union all 
select i.date,'0' as cost,i.income as income 
from test.incomes as i 
where i.date not in (select e.date from test.expenses as e); 

在这里,我创建了一个名为expensesincomes与在表中给出的字段两个表指定above.And我用的是上面提到的查询和结果如下。

enter image description here

我希望这是你所期望的。

+0

感谢阿肖克纪... – user2851129

+0

选择expenses.date,随着收入 从支出 expenses.cost, '0',其中在(选择收入incomes.date)expenses.date不 UNION ALL 选择不同的(费用.date),expenses.cost,从费用incomes.cost 内从收入 其中收入加入收入上expenses.date = incomes.date UNION ALL 选择incomes.date, '0' 作为成本,incomes.cost .date不在(select expenses.date from expenses) esma distinct date haruko sum kasari nikalneho – user2851129

+0

select expenses.date,expenses.cost,'0'作为收入 来自费用 其中expenses.date不在(select incomesdate from incomes) union all select distinct(expenses.date),sum(expenses.cost, ),总和(收入。从费用 内成本) 加入收入上expenses.date = incomes.date UNION ALL 选择incomes.date, '0' 作为成本,incomes.cost 从收入 其中incomes.date未在(选择expenses.date来自费用) esari sum garna khojda问题aauncha – user2851129