MySQL使用GROUP BY分组出错
SELECT list is not in GROUP BY clause and contains nonaggregated column 'tb_leixing.category_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
语句
SELECT * FROM tb_leixing GROUP BY category_id;
查询出的列我用的*代表所有的属性,就报上述错,我把*改成category_id就OK了。
GROUP BY 用法
GROUP BY,即以其中一个字段的值来分组。
select 的字段只能是分组的字段类别以及使l聚合函数如,max(),min(),count()的字段。
where在前,group by在后的原因:要先用where过滤掉不进行分组的数据,然后在对剩下满足条件的数据进行分组。
having是在分好组后找出特定的分组,通常是以筛选聚合函数的结果,如sum(a) > 10等,且having必须在group by 后面,
使用了having必须使用group by,但是使用group by 不一定使用having。不允许使用双重聚合函数,所以在对分组进行筛选的时候,可以用order by 排序,然后用limit也可以找到极值。到极值。