0

I have a SQL SELECT query that works in mySQL, both from PHPmyadmin and from the console. However, when it was converted to Laravel query builder, it borked with the following error message:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not
in GROUP BY clause and contains nonaggregated column 'wcce_dev.client_contacts.family_name'
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with 
sql_mode=only_full_group_by 

The following is the SQL SELECT query:

SELECT `clients`.name, `clients`.address_1, `client_contacts`.family_name,
  `client_contacts`.given_name, `client_contacts`.office_country,
  `client_contacts`.office_city, `client_contacts`.office_number, `client_contacts`.position, `contact_titles`.description
FROM `clients`
INNER JOIN `client_contacts`
ON `clients`.entry = `client_contacts`.client
INNER JOIN `contact_titles`
ON `client_contacts`.title = `contact_titles`.code
GROUP BY `clients`.entry
ORDER BY `clients`.entry ASC;

The following is the Laravel query builder statement:

DB::table('clients')
->select('clients.name', 'clients.address_1', 'client_contacts.family_name',
  'client_contacts.given_name', 'client_contacts.office_country', 
  'client_contacts.office_city', 'client_contacts.office_number', 
  'client_contacts.position', 'contact_titles.description')
->join('client_contacts','clients.entry','=','client_contacts.client')
->join('contact_titles','client_contacts.title','=','contact_titles.code')
->groupBy('clients.entry')
->orderBy('clients.entry','asc')
->get();

It's very strange. Any help is greatly appreciated.

2
  • Add clients.entry field to your select query. and try Commented Apr 24, 2023 at 6:10
  • Same error? Then try SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); or direct change it from MySql settings. Commented Apr 24, 2023 at 7:07

2 Answers 2

0

The problem is that your related table client_contacts can have more than one row which corresponds to a row in the clients table. Even if it won't happen in reality, the db doesn't know that and throws an error.

You have two options

  1. Turn off sql_mode=only_full_group_by so that it can pick one row at random and display that
  2. Precede each aggregate column with MAX() so that it just gets one of them.

The resulting query would look like this:

SELECT `clients`.name, `clients`.address_1, 
  MAX(`client_contacts`.family_name),
  MAX(`client_contacts`.given_name), 
  MAX(`client_contacts`.office_country),
  MAX(`client_contacts`.office_city), 
  MAX(`client_contacts`.office_number),   
  MAX(`client_contacts`.position), 
  MAX(`contact_titles`.description)
FROM `clients`
INNER JOIN `client_contacts`
ON `clients`.entry = `client_contacts`.client
INNER JOIN `contact_titles`
ON `client_contacts`.title = `contact_titles`.code
GROUP BY `clients`.entry
ORDER BY `clients`.entry ASC;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this command in mysql, phpmyadmin then try

SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode, ',ONLY_FULL_GROUP_BY'));

And if you using group by then group by column must be present in select query "'clients.entry'"

DB::table('clients')
->select('clients.name', 'clients.address_1', 'client_contacts.family_name',
  'client_contacts.given_name', 'client_contacts.office_country', 
  'client_contacts.office_city', 'client_contacts.office_number', 
  'client_contacts.position', 'contact_titles.description', 'clients.entry')
->join('client_contacts','clients.entry','=','client_contacts.client')
->join('contact_titles','client_contacts.title','=','contact_titles.code')
->groupBy('clients.entry')
->orderBy('clients.entry','asc')
->get();

1 Comment

Still doesn't work :( What really annoys me is why it works in mySQL 8 directly, but not through Laravel 10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.