基于另一列laravel

时间:2016-09-30 07:10:47

标签: php mysql laravel laravel-5

我想将列AMOUNT加起来,结果填写BALANCE列。如果列TYPE是DEBIT,那么它将总和..但如果列TYPE是KREDIT,那么它将是负数。这是表table image

创建该表,我在laravel controller中使用了这个查询:

$get_result        =  DB::select( DB::raw("SELECT statement.created_at, statement.descript, statement.amount, statement.sign, statement.reference 
                                                    FROM statement,lender 
                                                    WHERE statement.created_at BETWEEN DATE_ADD(' $date_from ',INTERVAL 1 DAY) 
                                                    AND '$date_to' 
                                                    AND statement.lender_id = lender.id 
                                                    AND lender.user_id= $userId ") );

我在视图中使用此代码来显示数据:

<thead>
                                <tr class="txtcenter">
                                    <th class="all">Date </th>
                                    <th class="all">Description </th>
                                    <th class="all">Amount</th>
                                    <th class="all">Type</th>
                                    <th class="all">Reference</th>
                                    <th class="all">Balance</th>
                                </tr>
                            </thead>
                            <tbody>@foreach($get_result as $statement)
                                    <tr class="accordion">
                                        <td>{{SUBSTR($statement->created_at,0,10) }}</div>
                                        <td>{{$statement->descript}} </div>
                                        <td>IDR {{number_format($statement->amount, 0, '.', ',')}}  </div> 
                                        <td>{{ $statement->sign }}  </div>
                                        <td>{{ $statement->reference }} </div>
                                        <td>  </div>
                                    </tr>@endforeach
                            </tbody>

我不知道如何制作正确的代码来计算所有内容。 请帮我。 提前致谢

2 个答案:

答案 0 :(得分:0)

尝试此查询

SELECT statement.created_at, statement.descript, statement.amount, statement.sign, statement.reference, (sum(case when statement.descript = 'Credit' then statement.amount else 0 end) - sum(case when statement.descript = 'Debit' then statement.amount else 0 end)) balance FROM statement,lender WHERE statement.created_at BETWEEN DATE_ADD(' $date_from ',INTERVAL 1 DAY) AND '$date_to' AND statement.lender_id = lender.id AND lender.user_id= $userId

答案 1 :(得分:0)

&#34; classic&#34;这种方式(这是运行总和的特殊情况)是使用相关查询:

SELECT statement.created_at, statement.descript, statement.amount, statement.REFERENCE,
    ( SELECT 
      SUM(CASE WHEN t2.descript = 'Debit'  THEN t2.amount 
               WHEN t2.descript = 'Credit' THEN -t2.amount ELSE 0 END ) 
      FROM statement t2
      WHERE t2.created_at <= statement.created_at
    ) AS Balance
FROM statement
ORDER BY statement.created_at ;

或使用如下的sql变量:

SET @csum := 0;
SELECT statement.created_at, statement.descript, statement.amount,
       statement.REFERENCE,
    (CASE WHEN statement.descript = 'Debit' THEN
            (@csum := @csum + statement.amount)  
          WHEN statement.descript = 'Credit' THEN 
            (@csum := @csum - statement.amount)  
         ELSE  0  
     END) as Balance
FROM statement
ORDER BY statement.created_at ;

检查sqlFiddle是否有正常工作的代码 - 不包括贷款人加入,还有其他标准。