+ -
当前位置:首页 → 问答吧 → 关于mysql两查询结果的左连接问题

关于mysql两查询结果的左连接问题

时间:2011-07-25

来源:互联网

现在在作大作业
有两个查询结果

A:
select exam_student.studentID,
       student.loginName,
       student.trueName,
       student.class
        from exam_student,exam,student where exam_student.examID=1 and exam_student.examID=exam.id and exam_student.studentID=student.id group by exam_student.studentID


B:
select        answer.studentID,
        answer.pageID,
        sum(answer.score)
        from answer, student where student.id=answer.studentID group by answer.studentID) on atable.exam_student.studentID=btable.answer.studentID;

我想把这两个查询结果左连接成一个查询结果,但语法怎么写都不对,对了结合条件是 A中exam_student.studentID= b中answer.studentID,用的数据库是mysql
求大神指导,不甚感激!

作者: saul_yuan   发布时间: 2011-07-25

没人回答么!?还是我问得有问题!?

作者: saul_yuan   发布时间: 2011-07-25

在SELECT子句中出现的字段,如果不在GROUP BY子句中出现,就要对它使用聚合函数,否则在mysql服务器默认的sqlmode下会报错。
以查询第一个表为例,可以改成:
select exam_student.studentID,
       MAX(student.loginName),
       MAX(student.trueName),
       MAX(student.class)
        from exam_student,exam,student where exam_student.examID=1 and exam_student.examID=exam.id and exam_student.studentID=student.id group by exam_student.studentID
但是这样产生的结果集里面,另外三个字段值不确定。
我认为你对自己想要什么样的结果集并不特别清楚,你可以好好琢磨一下你要的结果是什么样子的~

作者: liuxinran819   发布时间: 2011-07-25