S
S
semki0962018-03-14 15:46:16
MySQL
semki096, 2018-03-14 15:46:16

How to make a database query and select the maximum and minimum value?

Guys help me figure it out. I make a selection of temperatures from the database - for the current day in segments of 3 hours. Like this (probably you can somehow optimize the query, I will be grateful for the comments)

//данные на сегодня - на временных отрезках, например today0_3 - сегодня с нуля часов до 3 часов итд.
    $today0_3 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 00:00:00')->where('time', '<=', date('Y-m-d').' 03:00:00')->get();
    $today3_6 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 03:00:00')->where('time', '<=', date('Y-m-d').' 06:00:00')->get();
    $today6_9 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 06:00:00')->where('time', '<=', date('Y-m-d').' 09:00:00')->get();
    $today9_12 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 09:00:00')->where('time', '<=', date('Y-m-d').' 12:00:00')->get();
    $today12_15 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 12:00:00')->where('time', '<=', date('Y-m-d').' 15:00:00')->get();
    $today15_18 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 15:00:00')->where('time', '<=', date('Y-m-d').' 18:00:00')->get();
    $today18_21 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 18:00:00')->where('time', '<=', date('Y-m-d').' 21:00:00')->get();
    $today21_24 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 21:00:00')->where('time', '<=', date('Y-m-d').' 24:00:00')->get();

Now I need to make a selection in each time interval - the lowest temperature and the highest temperature in this segment. How to do it (I am using Eloquent Laravel ORM)? Maybe it's better to do it in PHP or with a query?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andreo, 2018-03-14
@semki096

$today0_3->min();
$today0_3->max();
# И так дальше ...

Preved, doka

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question