读书人

SQL语句有关问题【急】

发布时间: 2013-08-04 18:26:15 作者: rapoo

SQL语句问题【急。。。。。。。。。。。。。。。。。。。。。。】
子查询的数据小于外查询的数据不一致怎么办?
例如:
学生表(student):
StudentID name age class Grade
1 张三 16 1 4
2 李四 15 5
3 王五 17
4 赵六 16 2

班级/年级表(class):
classID type className
1 class 1班
2 class 2班
3 class 3班
4 grade 5年级
5 grade 6年级

我想要的查询结果:
StudentID name age className grade
1 张三 16 1班 5年级
2 李四 15 6年级


3 王五 17
4 赵六 16 2班

我的查询语句:
select studentId,name,age,(select className from class where classId = a.class and type = 'class') className,(select className from class where classId = a.Grade and type = 'grade') grade from student a

Ps:因为学生表中班级和年级字段有的是空的,所以子查询的数据少了,报错:值过多。

如果想得到我要的查询结果该怎么查?

[解决办法]
可以试下这样,(没有测试过)
select A.StudentID,
A.name,
A.age,
B.classname,
b.grade
from student a left outer join class b
on a.class=b.classID

读书人网 >oracle

热点推荐