Mysql query error :SELECT list is not in GROUP BY clause and contains nonagregated column, incompatible with sql_mode=only_full_group_by

msp :

I am fetching records from two table by joining them i.e product table will be join with rating table and getting product with its average rating, but unfortunately it is not working on live server however it is working perfectly on localhost so I followed below links but no change still getting same error, so finally I realize it is query issue ,because localhost it is working perfectly and If I try to make changes in sql mode it is not allowed in case of live mysql server and showing preveliges restriction, so what changes require in query to get result properly.

product

id     product_name    keyword

1        A               xyz
2        B               abc
3        C               aaa
4        D               abc

rating

id      product_id    rating  

1          2             2
2          2             4
3          1             2
4          4             3
5          2             3
6          2             4

Mysql query:

select p.pid,
       p.product_name, 
       COALESCE(ROUND(AVG(r.rating), 1), 0) as avgRate 
from product p 
left join rating r on r.product_id=p.id  
WHERE (LOWER(p.product_name) LIKE '%a%'  
   OR LOWER(p.keyword) LIKE '%abc%' )
GROUP BY p.product_id

Referred Links:

Error related to only_full_group_by when executing a query in MySql.

#1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this is incompatible with sql_mode=only_full_group_by

Akina :

Use ANY_VALUE() function:

select ANY_VALUE(p.pid) pid,
       ANY_VALUE(p.product_name) product_name, 
       COALESCE(ROUND(AVG(r.rating), 1), 0) as avgRate 
from product p 
left join rating r on r.product_id=p.id  
WHERE (   LOWER(p.product_name) LIKE '%a%'  
       OR LOWER(p.keyword) LIKE '%abc%' )
GROUP BY p.product_id

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19452&siteId=1