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

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

我正在通过加入两个 table 来获取记录,即产品 table 将与评级 table 联合并获得具有平均评级的产品,但不幸的是它不起作用在实时服务器上,它在本地主机上运行良好,所以我按照下面的链接进行操作,但没有任何更改仍然出现相同的错误,所以最后我意识到这是查询问题,因为本地主机运行良好,如果我尝试在 sql 模式在实时 mysql 服务器的情况下是不允许的,并且显示 preveliges 限制,因此查询需要进行哪些更改才能正确获得结果。

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查询:

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

参考链接:

.

您需要将所有非聚合列添加到分组依据

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.pid,p.product_name

使用ANY_VALUE()函数:

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

请在此处检查解决方案。设置系统变量并从系统变量中删除ONLY_FULL_GROUP_BY

Disable ONLY_FULL_GROUP_BY