0

I have the following query (UPDATED SEE BELOW):

SELECT 

i0_.id AS id_0, 
i0_.address AS address_1, 
i1_.name AS name_2, 
c2_.name AS name_3, 
c3_.code AS code_4, 
l4_.iso AS iso_5, 
c5_.id AS id_6, 
c6_.name AS name_7, 
i7_.identifier AS identifier_8 

FROM institutions i0_ 
LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id) 
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id) 
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id) 
LEFT JOIN country_spoken_languages c8_ ON (c8_.country_id = c3_.id) 
LEFT JOIN cities c5_ ON (c5_.id = i0_.city_id) 
LEFT JOIN city_languages c6_ ON (c6_.city_id = c5_.id) 
LEFT JOIN languages l4_ ON (l4_.id = i1_.language_id) 
LEFT JOIN institution_types i7_ ON (i0_.institution_type_id = i7_.id) 

WHERE c8_.is_primary = 1 
AND c8_.language_id = l4_.id 
AND c2_.language_id = 546 
AND i7_.identifier = "work_place" 
GROUP BY id_6 #here is the issue...

UPDATED Query

SELECT 
i0_.id AS id_0, 
i0_.address AS address_1, 
i1_.name AS name_2, 
c2_.name AS name_3, 
c3_.code AS code_4, 
c4_.name AS name_5, 
i5_.identifier AS identifier_6 

FROM institutions i0_ 

LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id) 
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id) 
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id AND c2_.language_id = ?) 
LEFT JOIN country_spoken_languages c6_ ON (c6_.country_id = c3_.id AND c6_.language_id = ? AND c6_.is_primary = ?) 
LEFT JOIN city_languages c4_ ON (c4_.city_id = i0_.city_id) 
LEFT JOIN institution_types i5_ ON (i0_.institution_type_id = i5_.id) 

WHERE i5_.identifier = ? 
GROUP BY i0_.city_id

This query is having a problem with GROUP_BY which I am not sure how to solve:

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.i0_.id' which is not

functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

I know this can be easily solved by setting off the only_full_group_by but what can i do to my query to make it work properly and not having to modify the MySQL setup on my server?

2
  • 2
    You can place all columns in your SELECT other than c5_.id into aggregate functions. The error you are getting is sort of telling you to do this. You could also remove those other columns. Commented May 13, 2017 at 9:57
  • Your left joins are superfluous because your where clause turns them to inner joins. Commented May 13, 2017 at 10:07

2 Answers 2

3

if you want not change the sql_mode=only_full_group_by

you can simply add an aggegation function to the column not involved in group by ( eg min() or max()

(in the previuos versione the result for this column was impredictable. in this way you assign a rule for get the value for these columns )

SELECT 
  i0_.id AS id_0, 
  min(i0_.address AS) address_1, 
  min(i1_.name) AS name_2, 
  min(c2_.name )AS name_3, 
  min(c3_.code) AS code_4, 
  min(c4_.name) AS name_5, 
  min(i5_.identifier) AS identifier_6 

FROM institutions i0_ 

LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id) 
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id) 
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id AND c2_.language_id = ?) 
LEFT JOIN country_spoken_languages c6_ ON (c6_.country_id = c3_.id AND c6_.language_id = ? AND c6_.is_primary = ?) 
LEFT JOIN city_languages c4_ ON (c4_.city_id = i0_.city_id) 
LEFT JOIN institution_types i5_ ON (i0_.institution_type_id = i5_.id) 

WHERE i5_.identifier = ? 
GROUP BY i0_.city_id
0

I think you should use at least on aggregate function in select field for the query to work, see below i tried with a sample database 'classicmodels' from tables 'customers' and 'orders':

select c.customerNumber, sum(o.orderNumber)
from customers c
join orders o
on(c.customerNumber = o.customerNumber)
group by c.customerNumber;

the result is returned but if i remove the function 'sum()' the out is as:

ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'classicmodels.o.orderNumber' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Not the answer you're looking for? Browse other questions tagged or ask your own question.