免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3156 | 回复: 4
打印 上一主题 下一主题

关于mysql两查询结果的左连接问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-25 13:28 |只看该作者 |倒序浏览
现在在作大作业
有两个查询结果

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
求大神指导,不甚感激!

论坛徽章:
0
2 [报告]
发表于 2011-07-25 14:40 |只看该作者
没人回答么!?还是我问得有问题!?

论坛徽章:
0
3 [报告]
发表于 2011-07-25 16:26 |只看该作者
在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
但是这样产生的结果集里面,另外三个字段值不确定。
我认为你对自己想要什么样的结果集并不特别清楚,你可以好好琢磨一下你要的结果是什么样子的~

论坛徽章:
0
4 [报告]
发表于 2011-07-25 16:37 |只看该作者
回复 3# liuxinran819

推荐文章:

剖析:MySQL的左连接、右连接、等值连接异同
链接地址:http://www.mysqlops.com/2011/03/ ... ght-inner-join.html

论坛徽章:
0
5 [报告]
发表于 2011-07-26 09:48 |只看该作者
回复 3# liuxinran819


    这...有关到左连接么!?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP