问题:
[SQL] SELECT pi.paper_interviewee_id, pi.batch, pi.total_time,pi.use_time, pq.question_score, sum(pq.question_score), q.classification_id, qc.classification_name
from t_paper_interviewee pi, t_paper_question pq, t_question q, t_question_classification qc WHERE pi.interviewee_id="1" and pi.correct = 1
and q.question_id = pq.question_id and qc.classification_id = q.classification_id
and pi.paper_id = pq.paper_id GROUP BY q.classification_id
[Err] 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'clh-zq.pi.paper_interviewee_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by、
原因:
数据库版本的问题
MySQL 5.7.5和up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。(在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅MySQL 5.6参考手册。)。
解决:
SELECT sum(pq.question_score), pi.paper_interviewee_id, pi.batch, pi.total_time,pi.use_time, q.classification_id, qc.classification_name
from t_paper_interviewee pi, t_paper_question pq, t_question q, t_question_classification qc WHERE pi.interviewee_id="1" and pi.correct = 1
and q.question_id = pq.question_id and qc.classification_id = q.classification_id
and pi.paper_id = pq.paper_id GROUP BY pi.paper_interviewee_id, pi.batch, qc.classification_name ,q.classification_id