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>
结果是:
希望的输出是这样的..
] 3
感谢阿肖克纪... – user2851129
选择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
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