skip to Main Content

I need to run a custom query and I have tried all these methods

$sql = "SELECT acd.*, DATE_FORMAT(acd.cdate, '%d/%m/%Y') cdate_disp,
  GROUP_CONCAT(CONCAT_WS(', ', car.type, car.session, crd.name) SEPARATOR '<br />') rd_names,
  acb.booking_status my_booking_status
FROM app_data acd
INNER JOIN crmaccounts ca ON ca.id = acd.client_crm_id
LEFT JOIN crmaccount_rdids car ON car.account_id = ca.id
LEFT JOIN crmrd_ids crd ON crd.id = car.rd_id
LEFT JOIN app_bookings acb ON acb.call_ref_id = acd.call_ref AND acb.user_id = 12391
WHERE 1=1 
AND acd.client_crm_id NOT IN (select account_id from bstaff WHERE user_id=12391)
GROUP BY acd.id
ORDER BY acd.cdate, ctiming";

DB:select($sql);

throws

Illuminate Database QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 ‘mydatabase.acd.call_ref’ isn’t in GROUP BY (SQL: ….)

DB::select($sql);

DB::raw($sql);

DB::select(DB::raw($sql));

//even with pdo 
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_OBJ);

 PDOException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY

It seems like the only way to get working is to list all the table columns in the group by which is doable but not convenient.

I am able to run the same query directly in the phpmyadmin. So I am not sure why when I run it through Laravel it asks me add all columns.

I have MariaDB installed and both Laravel and PhpMyAdmin are connecting to the same instance. Larvel version is 5.8.5.

Please take a look at full query as I asked question here too but couldn’t find any answer – https://laracasts.com/discuss/channels/eloquent/query-runs-ok-in-phpmyadmin-but-dbselect-throws-exception

3

Answers


  1. You should try disabling the strict mode in config/database.php, in mysql connection section.

    Login or Signup to reply.
  2. This is not a Laravel issue, but a logical limitation by MySQL. When you add columns to your results set that are not in the GROUP BY clause, different values could be possible for these columns in the result set.

    When executing queries in strict mode (which is on by default), MySQL will not allow this because it could lead to unexpected results. If you turn off strict mode MySQL will use the first result it finds for these columns.

    You can turn off strict mode in your Laravel config files in config/database.php. However, it is recommended to change your query instead because of the unpredictability.

    A full list of checks in strict mode can be found in the MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict

    Login or Signup to reply.
  3. I have installed Laravel 4.8 and also configured Auth. further more,I add two tables called as role and states and tried your pdo code – its perfectly works for me! Here, I put my code.

    $sql = "SELECT * 
                FROM users u
                INNER JOIN roles r ON r.id = u.role_id
                LEFT JOIN states s ON s.id = u.state_id
    
                WHERE 1=1 
    
                GROUP BY u.id
                ORDER BY u.id ";
    
        $sth = DB::getPdo()->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll(PDO::FETCH_OBJ);
    
        dd($data);
    

    I am eager to know what is your db structure.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search