简体   繁体   中英

How to add extra minutes to time data type while sql query

I want to add extra 30mins to "daily_attendances.in_time" while counting "delay day". Any idea how to do that?

$attendance_list = DB::table('daily_attendances')
        ->join('employees', 'employees.card_id', '=', 'daily_attendances.card_id')
        ->select('daily_attendances.*','employees.basic_salary','employees.start_time','employees.over_time_rate',
            DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.delay_time ) ) ) AS delay_time'),
            DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.extra_time ) ) ) AS extra_time'),
            DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.in_use_time ) ) ) AS duty_time'),
            DB::raw('count(daily_attendances.start_time < daily_attendances.in_time) AS delay_day'),
            DB::raw(DB::raw('count(*) as present_day')))
        ->whereRaw($where)
        ->groupBy('daily_attendances.card_id')->get();

For this case we have to use MySQL function called DATEADD

Example:

SELECT DATE_ADD("2017-01-15 02:12:19", INTERVAL 30 MINUTE);

Output:

2017-01-15 02:42:19

I have modified your query, Please try it out.

$attendance_list = DB::table('daily_attendances')
  ->join('employees', 'employees.card_id', '=', 'daily_attendances.card_id')
  ->select('daily_attendances.*','employees.basic_salary','employees.start_time','employees.over_time_rate',
      DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.delay_time ) ) ) AS delay_time'),
      DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.extra_time ) ) ) AS extra_time'),
      DB::raw('SEC_TO_TIME( SUM( TIME_TO_SEC( daily_attendances.in_use_time ) ) ) AS duty_time'),
      DB::raw('count(DATE_ADD(daily_attendances.start_time, INTERVAL 30 MINUTE) < daily_attendances.in_time) AS delay_day'), //change over here
      DB::raw(DB::raw('count(*) as present_day')))
        ->whereRaw($where)
        ->groupBy('daily_attendances.card_id')->get();

You can also refer to the link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM