简体   繁体   中英

How to get the first data and last data from the same data in the database?

I want to output the time in and time out from this database. 在此处输入图片说明

So i want to get the first data and last data in date 2017-06-13 and 2017-06-15. How can i do that using Laravel 5.3?

May be this will work for you:

DB::table('emp')
  ->select(DB::raw("MIN(`time`) AS `time_in`, MAX(`time`) AS `time_out`"))
  ->where('date', '2017-06-13')
  ->get();

Output:

{
    time_in: "19:38:33",
    time_out: "22:14:10"
}

Try Below Code :

$attendancehours = DB::select(
    DB::RAW('TIMESTAMPDIFF(HOUR,CONCAT(in_date," ",in_time),CONCAT(out_date," ",out_time)'))->
    table('staff_attendances')->
    whereBetween('in_date', array($date1, $date2))->
    where('id', $sID)
    ->get();

You can order your table by the date and time columns and then use Eloquent to take ->first() and ->last() using those methods.

$collection = BiometricsAttendance::where('emp_id', $emp)->whereBet‌​ween('date', [$start, $end])->groupBy('date')->orderBy('time', 'asc');

$timein = $collection->first();

$timeout = $collection->last();

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