Mam taki problem mam sobie wyszukiwanie danych i do tej pory miałem tak, że mi dane rekordy dublowało mi w zalezności ile leków ma dany rekord przypisane, więc, żeby to naprawić to dałem
$this->qestion->groupBy("forwarding_drugs.id_mood");
To naprawiłom problem tylko teraz nie drukuje mi danych które nie mają żadnego leku
Więc dorzuciłem instrukcje warunkową w sql
$this->qestion->selectRaw("CASE WHEN forwarding_drugs.id_mood = null THEN '0' else forwarding_drugs.id_mood = forwarding_drugs.id_mood END) as drugsGroup");
$this->qestion->groupBy("drugsGroup");
Ale po tej operacji mam coś takiego
Column not found: 1054 Unknown column 'drugsGropup' in 'group statement'
Mam tabele takie tabela forwarding_drugs
idPodstawowy int(11) Nie Brak AUTO_INCREMENT Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
2 id_drugs int(11) Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
3 id_mood int(11) Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
4 id_user int(11)
Tabela drugs
idPodstawowy int(11) Nie Brak AUTO_INCREMENT Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
2 name varchar(100) latin1_swedish_ci Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
3 dose float Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
4 date datetime Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
5 id_users int(11) Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
6 portion tinyint(4)
Tabela moods
1 idPodstawowy int(11) Nie Brak AUTO_INCREMENT Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
2 date_start datetime Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
3 date_end datetime Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
4 id_users int(11) Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
5 level_mood float Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
6 what_work text utf8mb4_unicode_ci Tak NULL Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
7 level_anxiety float Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
8 level_nervousness float Nie Brak Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
9 epizodes_psychotik smallint(6) Tak NULL Zmień Zmień Usuń Usuń
Podstawowy Podstawowy
Jednoznaczny Jednoznaczny
Indeks Indeks
Więcej
10 level_stimulation float
Kod źródłowy
public function createQuestion($page,$start,$id) {
$hourStart = explode(":",$start);
$hour = $start;
$this->qestion = Moods::query();
$this->qestion->select(DB::Raw("(DATE(IF(HOUR(moods.date_start) >= '$hour', moods.date_start,Date_add(moods.date_start, INTERVAL - 1 DAY) )) ) as dat "));
$this->qestion->selectRaw("CASE WHEN forwarding_drugs.id_mood = null THEN '0' else forwarding_drugs.id_mood = forwarding_drugs.id_mood END as drugsGropup");
$this->qestion->selectRaw("hour(date_start) as hour");
$this->qestion->selectRaw("level_mood as level_mood");
$this->qestion->selectRaw("level_anxiety as level_anxiety");
$this->qestion->selectRaw("level_nervousness as level_nervousness");
$this->qestion->selectRaw("level_stimulation as level_stimulation");
$this->qestion->selectRaw("epizodes_psychotik as epizodes_psychotik");
$this->qestion->selectRaw("date_start as date_start");
$this->qestion->selectRaw("date_end as date_end");
$this->qestion->selectRaw("what_work as what_work");
$this->qestion->selectRaw("moods.id as id");
$this->qestion->selectRaw("forwarding_drugs.id_drugs as drugs");
$this->qestion->selectRaw("year((DATE(IF(HOUR(moods.date_start) >= '$hour', moods.date_start,Date_add(moods.date_start, INTERVAL - 1 DAY) )) )) as year");
$this->qestion->selectRaw("month((DATE(IF(HOUR(moods.date_start) >= '$hour', moods.date_start,Date_add(moods.date_start, INTERVAL - 1 DAY) )) )) as month");
$this->qestion->selectRaw("day((DATE(IF(HOUR(moods.date_start) >= '$hour', moods.date_start,Date_add(moods.date_start, INTERVAL - 1 DAY) )) )) as day");
$this->qestion->selectRaw("TIMESTAMPDIFF (SECOND, date_start , date_end) as divi");
$this->qestion->selectRaw("hour(date_start) as hour");
if (Input::get("moodForDay") != "") {
$this->qestion->selectRaw("round(sum(TIMESTAMPDIFF (SECOND, moods.date_start , moods.date_end) * moods.level_mood) / "
. "sum(TIMESTAMPDIFF(second,moods.date_start,moods.date_end)),2) as nas");
$this->qestion->selectRaw("round(sum(TIMESTAMPDIFF (SECOND, date_start , date_end) * level_anxiety) / "
. "sum(TIMESTAMPDIFF(second,date_start,date_end)),2) as nas2");
$this->qestion->selectRaw("round(sum(TIMESTAMPDIFF (SECOND, date_start , date_end) * level_nervousness ) / "
. "sum(TIMESTAMPDIFF(second,date_start,date_end)),2) as nas3");
$this->qestion->selectRaw("round(sum(TIMESTAMPDIFF (SECOND, date_start , date_end) * level_stimulation) / "
. "sum(TIMESTAMPDIFF(second,date_start,date_end)),2) as nas4");
}
$this->qestion->leftjoin("forwarding_drugs","forwarding_drugs.id_mood","moods.id");
if (Input::get("drugs") != "") {
$this->qestion->leftjoin("drugs","drugs.id","forwarding_drugs.id_drugs");
}
//if (count()) {
$this->setWhatWotk();
//}
$this->qestion->where("moods.id_users",$id);
if (Input::get("moodForDay") != "") {
$this->setGroup();
}
else {
$this->setWhere();
}
$this->setGroup2();
if (Input::get("drugs") != "") {
$this->setDrugs();
}
$this->setDate();
$this->sort($page);
$this->list = $this->qestion->Paginate(15);
}
private function setGroup2() {
$this->qestion->groupBy("drugsGropup");
}