2017-06-16 105 views
0

我想从这个数据库中输出时间和时间。 enter image description here如何从数据库中的相同数据获取第一个数据和最后一个数据?

所以我想要得到第一个数据和最后的数据在日期2017-06-13和2017-06-15。 我怎样才能使用Laravel 5.3?使用这些方法>末页() -

$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(); 
+0

与顺序运行查询的ASC和限制1,或ORDER BY DESC和限制1得到的结果。 – VK321

回答

0

试试下面的代码。

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

$timein = $collection->first(); 

$timeout = $collection->last(); 
0

您可以通过日期和时间列订购您的表,然后用雄辩拿 - >第一()和:

+0

$ timein = BiometricsAttendance :: where(array('emp_id'=> $ emp)) - > whereBetween('date',[$ start,$ end]) - > orderBy('date','asc') - > orderBy('time','asc') - > first(); $ timeout = BiometricsAttendance :: where(array('emp_id'=> $ emp)) - > whereBetween('date',[$ start,$ end]) - > orderBy('date','asc') - > orderBy('time','desc') - > first();从这个输出不是我想要的。第一个日期只是数据。我希望2017-06-13和2017-06-15也能输出 – Selle

+0

做groupBy('date') - > orderBy('time','asc'),你要确保日子一直排好队列 – btl

+0

我收到了2017-06-13和2017-06-15两个日期的第一个数据,但是最后一个数据我不能。使用first()和last()方法仅获取数据库中数据的第一行和最后一行。 – Selle

1

可能这会为你工作:

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

输出:

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

谢谢!这对我有效。非常感谢:) – Selle

+0

@Selle欢迎) –

+0

如果它适合你,然后标记为正确的PLZ –

相关问题