如果常用则使用日期字段合并表,如果日期字段不常见则添加新行

时间:2016-07-27 09:37:20

标签: arrays json laravel merge

我正在开发房屋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;
 }
);
};

查看部分:

 <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>

以json形式进行上述查询的结果是:

enter image description here

查看是这样的,没有显示收入: enter image description here

所需的输出就是这样..

enter image description here] 3

1 个答案:

答案 0 :(得分: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的表,其中包含您在上面指定的表中的给定字段。我使用上面提到的查询,结果如下。

enter image description here

我希望这是你所期望的。