Mam takie pytanie czy można jakoś za pomocą modelu wykonać select i w selecie jeszcze jeden select mam coś takiego.
$array = $Mood->select( DB::raw(" select date_start from sleeps where date_start >= '$this->dateStart' and date_end <= '$this->dateTo' and id_users = '$idUser'" ))
->where("date_start",">=",$this->dateStart)
->where("date_end","<=",$this->dateTo)
->where("id_users",$idUser)
->groupBy(DB::Raw("(DATE(IF(HOUR(date_end) >= '$hour', date_start,Date_add(date_end, INTERVAL - 0 DAY) )) ) "))
->get();
Bo to zwraca błąd
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select date_start from sleeps where date_start >= '2021-04-01' and date_end <= '' at line 1 (SQL: select select date_start from sleeps where date_start >= '2021-04-01' and date_end <= '2021-05-15' and id_users = '38' from `moods` where `date_start` >= 2021-04-01 and `date_end` <= 2021-05-15 and `id_users` = 38 group by (DATE(IF(HOUR(date_end) >= '5', date_start,Date_add(date_end, INTERVAL - 0 DAY) )) ))
I czy to jest dobrze jak na model czy lepiej użyć czegoś takiego
DB::select("select ...")
Bo chcę osiągnąć taki efekt, że mam na końcu dnia nastroj i jest record który ma początek nastroju i koniec nastroju i tych nastroi jest w ciągu dnia n, i jest jeszcze tabela sen i ta tabela ma począttek snu i koniec snu i tam najczęściej w ciągu dnia jest 1 sen i teraz chodzi mi, że skontruować zapytanie, które obliczy różnice minut w najpóźniejszym nastroju dnia i początkiem snu czyli jak mamy nastrój 21:00-22:00 i potem sen 23:30-8:50 to, żeby odjął 23:30 i 22:00 i teraz jak to zrobić najprościej mysql bo te tabele nie są powiązane myślałem, żeby zrobić select i drugi select w tym samym zapytaniu, więc jak to najprościej zrobić ?