欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

程序员文章站 2022-07-15 12:27:04
...

在mysql中使用 group by 出现的问题 

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

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

问题原因: 
ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现,否则就会报错,或者这个字段出现在聚合函数里面。 

在select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;要么就要被包含在聚合函数中。

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误  

找原因:

查看mysql版本命令:select version(); 

查看sql_model参数命令: 
SELECT @@GLOBAL.sql_mode;

SELECT @@SESSION.sql_mode; 

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

查看:
 ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
第一项默认开启ONLY_FULL_GROUP_BY了,导致出现了错误,需要把它的默认关掉。

解决方法: 
命令行输入 :
 set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

结果:

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/free377096858">
                <img src="https://profile.csdnimg.cn/C/7/0/3_free377096858" class="avatar_pic" username="free377096858">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit vip-tit"><a href="https://blog.csdn.net/free377096858" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">arctan90°</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog4.png">                                            </div>
                <div class="text"><span>原创文章 76</span><span>获赞 73</span><span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">关注</a>
                                                            <a href="https://im.csdn.net/im/main.html?userName=free377096858" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>