【问题标题】:merging table using date field if common and add new row if date field is not common如果常见则使用日期字段合并表,如果日期字段不常见则添加新行
【发布时间】:2016-12-01 05:47:06
【问题描述】:

我正在开发房屋应用程序。我使用查询从不同的表名分别作为费用和收入来检索费用和收入的数据。 如果我只使用费用,那么它会打印出来,如果我也尝试混合收入值,它会给我带来问题。

代码:

 $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形式的查询结果为:

不显示收入的视图是这样的:

想要的输出是这样的..

]3

【问题讨论】:

    标签: arrays json laravel merge


    【解决方案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 的表,其中包含您上面指定的表中的给定字段。我使用上面提到的查询,结果如下。

    我希望这是你所期望的。

    【讨论】:

    • 选择费用.日期,费用.成本,'0'作为费用收入,其中费用.日期不在(从收入中选择收入)联合都选择不同的(费用.日期),费用。 cost,incomes.cost 来自费用内部连接的收入 costs.date = incomes.date union all selectincomes.date,'0' as cost,incomes.cost fromincomes whereincomes.date 不在(从费用中选择费用.date) esma distinct date haruko sum kasari nikalneho
    • select costs.date,expenses.cost,'0' asincome from costs where costs.date not in (selectincomes.date fromincomes) union all select distinct(expenses.date), sum(费用.cost), sum(incomes.cost) 从费用内部连接费用.date =incomes.date union all select incomes.date,'0' as cost,incomes.cost from incomes.date not in (select费用。日期从费用)esari sum garna khojda 问题 aauncha
    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多