SUM table in laravel 5

2019-09-05 00:51发布

here my table and what i want to do

SUM data column total ?for example i have two data in column total 01:45 and 02:25 result must be 4:10 . i have already try but my code only sum the hours not with the minute. what should do ?

here my controller. for sum

public function getMei()
    {
        $data['users'] = DB::table('lembur_karyawan')
                ->select('nama', DB::raw('SUM(total) as total_lembur'))
                ->groupBy('nama')
                ->havingRaw('SUM(total)')
                ->first();

        return view('mei_user', $data);
    }

and my view :

<?php echo $users->{'total_lembur'} ?>

2条回答
疯言疯语
2楼-- · 2019-09-05 01:22
public function getMei()
{
    $data['users'] = DB::table('lembur_karyawan')
            ->selectRaw('SEC_TO_TIME(SUM(timeSpent)) as  total_lembur') 
            ->groupBy('nama')
            ->havingRaw('SUM(total)')
            ->first();
    return view('mei_user', $data);
}
查看更多
对你真心纯属浪费
3楼-- · 2019-09-05 01:36

Use

DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) as total_lembur')

MySQL autoconverts time to hours and you have to explicitly convert value by either TIME_TO_SEC or UNIX_TIMESTAMP.

查看更多
登录 后发表回答