在3-22节最后一个练习,这里有个问题,如果是用MySQL默认的设置会报错
select t1.stu_no, t1.stu_name, sum(t2.score)
from school_student_info t1, school_student_grade t2
where t1.id = t2.student_id
group by t1.stu_no
order by sum(t2.score) desc;
错误是
1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘my_data_base.t1.stu_name’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
group by 的col是 stu_no, 但是显示的col包括stu_name, 但是它不包括在group by的语句里,所以和sql_mode的only_full_group_by不匹配,应该要设置一下
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
详细的看stackoverflow
如果用MariaDB就没有这个问题。讲师在这里应该说明一下