oracle一个简单的sql求助

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:54:48
表1
学生id 课程id
---------------
stu_1 course_1
stu_1 course_2
stu_1 course_4
stu_2 course_2
stu_2 course_3
stu_2 course_4
stu_3 course_2
stu_3 course_4

表2
课程id 老师id
-------------------
course_1 teacher_1
course_2 teacher_2
course_3 teacher_3
course_4 teacher_4

要得到的结果是结果
学生id 课程id 老师id
-----------------------------
stu_1 course_1 teacher_1
stu_1 course_2 teacher_2
stu_1 未休 未休
stu_1 course_4 teacher_4
stu_2 未休 未休
stu_2 course_2 teacher_2
stu_2 course_3 teacher_3
stu_2 course_4 teacher_4
stu_3 未休 未休
stu_3 course_2 teacher_2
stu_3 未休 未休
stu_3 course_4 teacher_4

------------------------------------------------
不要用
select 表1.学生id,表1.课程id,表2.老师id
from 表1,表2
where 表1.课程id(+) = 表2.课程id

select t1.学生id,
decode(t2.课程id,null,'未休',t1.课程id) as 课程id,
decode(t2.老师id,null,'未休',t2.老师id) as 老师id
from 表1 t1,表2 t2
where t1.课程id(+) = t2.课程id

oracle9以上
/****************************
select * from 表2 a
left join 表1 b on a.课程id =b.课程id

select 表1.学生id,表1.课程id,表2.老师id
from 表1,表2
where 表1.课程id(+) = 表2.课程id

这不就够了么

明白你意思了,感觉你就是把表2做为主表.