Column not found: 1054 Unknown column 'drugsGroup' in 'group statement' w laravel 5

0

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");
    }
0

A możesz podesłać, jakie zapytanie ostatecznie leci do bazy danych i co to za baza ?

0
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'drugsGroup' in 'group statement' (SQL: select count(*) as aggregate from `moods` left join `forwarding_drugs` on `forwarding_drugs`.`id_mood` = `moods`.`id` where `moods`.`id_users` = 38 group by `drugsGroup`)
0

Z tego co pamiętam, ->Paginate(...) czyści m.in. grupowanie - musiałbyś stworzyć nowe zapytanie, z tym Twoim oryginalnym będącym w podzapytaniu:

$paginatedResults =
  QueryBuilder::new()
    ->select('*')
    ->from('(' . $this->qestion->toSql() . ')');

return $paginatedResults->paginate(15);

(kod pisany z palca, więc prawdopodobnie ostatecznie wyjdą nieco inne nazwy metod)

0

Kolumna drugsGroup nie występuje w żadnej tabeli z tego zapytania, to po czym baza ma grupować? Z tego co widzę to ta kolumna nie występuje w twoim schemacie.

0

Zawsze na ten komunikat stosowałem

Zmiana z

$this->qestion->groupBy("drugsGroup");

na

  $this->qestion->groupBy("CASE WHEN forwarding_drugs.id_mood = null THEN '0'  else forwarding_drugs.id_mood = forwarding_drugs.id_mood  END");

Tylko teraz mam komunikat

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 '.`id_mood END`' at line 1 (SQL: select count(*) as aggregate from `moods` left join `forwarding_drugs` on `forwarding_drugs`.`id_mood` = `moods`.`id` where `moods`.`id_users` = 38 group by `CASE WHEN forwarding_drugs`.`id_mood = null THEN '0' else forwarding_drugs`.`id_mood = forwarding_drugs`.`id_mood END`)
0

Końcówka dla case to end case, nie samo end (https://dev.mysql.com/doc/refman/5.7/en/case.html).

0

Jeszcze nie doSZEDŁem do tego Próbuje zrobić coś z tym

$this->qestion->groupBy("moods.id");
        $this->qestion->havingRaw("CASE WHEN count(forwarding_drugs.id_mood) = 0 THEN  1  else forwarding_drugs.id_mood  END ");

Próbowałem tu różnych kombinacji chodzi o to, że jak zastosuje

$this->qestion->groupBy("forwarding_drugs.id_mood");

To mi drukuje tylko wpisy gdzie jest przypisany lek więc próbuje zastosowac coś takiego

$this->qestion->havingRaw("CASE WHEN count(forwarding_drugs.id_mood) = 0 THEN 1 else forwarding_drugs.id_mood END "); ale mi zwraca błąd.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'forwarding_drugs.id_mood' in 'having clause' (SQL: select count(*) as aggregate from `moods` left join `forwarding_drugs` on `forwarding_drugs`.`id_mood` = `moods`.`id` where `moods`.`id_users` = 38 group by `moods`.`id` having CASE WHEN count(forwarding_drugs.id_mood) = 0 THEN 1 else forwarding_drugs.id_mood END)

1 użytkowników online, w tym zalogowanych: 0, gości: 1