ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误
在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
问题原因:
ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现,否则就会报错,或者这个字段出现在聚合函数里面。
在select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;要么就要被包含在聚合函数中。
找原因:
查看mysql版本命令:select version();
查看sql_model参数命令:
SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;
查看:
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';
结果:
<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="{"mod":"popu_379","ab":"new"}" 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="{"mod":"popu_379","ab":"new"}">关注</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>