dadub's avatar
Level 1

SQL Error with GROUP BY

Hello,

I have this error :

```message "SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'project.hinteractions.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select hinteractions.id as hId, dinteractions.id as dId, hinteractions.notes as hNotes, dinteractions.notes as dNotes, targets.name as targetName, hforce.name as hForce, hforc….id = hinteractions.target_id inner join dinteractions on targets.id = dinteractions.target_id left join forces as hforce on hforce.id = hinteractions.force_id left join forces as dforce on dforce.id = dinteractions.force_id where hinteractions.herb_id = 36 and targets.id = 326 and (targets.target_type_id LIKE 2 or targets.target_type_id LIKE 3) group by hinteractions.notes order by dinteractions.force_id asc, hinteractions.force_id asc)"


After multiple searches, I know the cause of this error is these two lines :

``->groupBy('hinteractions.notes')
->groupBy('hinteractions.notes')

Here is the complete code of the SQL query :

${"result$i"} = DB::table('targets')
                        ->join('hinteractions', 'targets.id', '=', 'hinteractions.target_id')
                        ->join('dinteractions', 'targets.id', '=', 'dinteractions.target_id')
                        ->leftJoin('forces as hforce', 'hforce.id', '=', 'hinteractions.force_id')
                        ->leftJoin('forces as dforce', 'dforce.id', '=', 'dinteractions.force_id')

                        ->select('hinteractions.id as hId', 'dinteractions.id as dId', 'hinteractions.notes as hNotes', 'dinteractions.notes as dNotes', 'targets.name as targetName', 'hforce.name as hForce', 'hforce.color as hColor', 'dforce.name as dForce', 'dforce.color as dColor')
                        ->where('hinteractions.herb_id', $nbrPlantes[$i-1])
                        ->where('targets.id', $request->mechanismId1)
                        ->where(function ($query) {
                            $query->where('targets.target_type_id', 'LIKE' , 2)
                                    ->orWhere('targets.target_type_id', 'LIKE' , 3);
                            })
                        ->groupBy('hinteractions.notes')
                        ->orderBy('dinteractions.force_id', 'ASC')
                        ->orderBy('hinteractions.force_id', 'ASC')
                        ->get();

                    if (isset(${"result$i"}[0]) && !(${"result$i"}[0] == null)) {
                        $truc = ${"result$i"}[0];
                        ${"drug$i"} =  DB::table('drugs')->where('id', $truc->dId)->pluck('name');
                    }
                    else {${"drug$i"} = "";}
                          
                    ${"herb$i"} =  DB::table('herbs')->where('id', $nbrPlantes[$i-1])->pluck('name');
                    $retour['herb'.$i] = ${"herb$i"};
                    $retour['drug'.$i] = ${"drug$i"};
                    $retour['result'.$i] = ${"result$i"};
                    $retour['references'.$i] = ${"references$i"};            
                }
                $mechanism = DB::table('targets')->where('id', $request->mechanismId1)->pluck('name');
                $retour['mechanism'] = $mechanism;
                
            }
            else{ 
                $retour['mode_interaction'] = "multimechanisms"; 
                //Evenor S. pour chaque mechanism faire une requete sql pour avoir les interactions avec une plante
                for ($i=1; $i < $compteurMechanisms; $i++) {

                    ${"references$i"} = DB::table('references')
                        ->join('hinteraction_has_references', 'references.id', '=', 'hinteraction_has_references.reference_id')
                        ->join('dinteraction_has_references', 'references.id', '=', 'dinteraction_has_references.reference_id')
                        ->join('hinteractions', 'hinteractions.id', '=', 'hinteraction_has_references.hinteraction_id')
                        ->join('dinteractions', 'dinteractions.id', '=', 'dinteraction_has_references.dinteraction_id')
                        ->where('hinteractions.herb_id', $request->herbId1)
                        ->where('dinteractions.target_id', $nbrMechanisms[$i-1])
                        ->select('hinteractions.id as hId', 'dinteractions.id as dId', 'references.*')
                        ->get();
                    
                    
                    ${"result$i"} = DB::table('targets')
                        ->join('hinteractions', 'targets.id', '=', 'hinteractions.target_id')
                        ->join('dinteractions', 'targets.id', '=', 'dinteractions.target_id')
                        ->leftJoin('forces as hforce', 'hforce.id', '=', 'hinteractions.force_id')
                        ->leftJoin('forces as dforce', 'dforce.id', '=', 'dinteractions.force_id')

                        ->select('hinteractions.id as hId', 'dinteractions.id as dId', 'hinteractions.notes as hNotes', 'dinteractions.notes as dNotes', 'targets.name as targetName', 'hforce.name as hForce', 'hforce.color as hColor', 'dforce.name as dForce', 'dforce.color as dColor')
                        ->where('hinteractions.herb_id', $request->herbId1)
                        ->where('targets.id', $nbrMechanisms[$i-1])
                        ->where(function ($query) {
                            $query->where('targets.target_type_id', 'LIKE' , 2)
                                    ->orWhere('targets.target_type_id', 'LIKE' , 3);
                            })
                        
                        ->groupBy('hinteractions.notes')
                        ->orderBy('dinteractions.force_id', 'ASC')
                        ->orderBy('hinteractions.force_id', 'ASC')
                        ->get();

Have I to change my code ? Do you have any idea please ?

For information, I change my config database file :

'mysql' => [
            'driver' => 'mysql',
...
'strict' => false,

It still not working, I have to comment my two group by code lines

Thank you in advance for your help.
0 likes
2 replies
laracoft's avatar
  1. Check what is your current sql_mode using select @@sql_mode in some SQL client
  2. Copy the output and remove only_full_group_by
  3. Find the line sql_mode in your /etc/my.cnf and set it to the output e.g. sql-mode ="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
  4. Restart your mysql, e.g. service mysqld restart

Please or to participate in this conversation.