表的结构和要求如下:
表名:Studentinfo:
studentid name sex
001 张三 男
002 赵四 女
表名:Coursescore
Studentid maths english
001 80 90
002 75 95
表名:Teacherinfo
teacherid tname course
101 美女 maths
102 帅哥 english想要的结果:请各位指导SQL如何写:
Studentid name sex course score tname
001 张三 男 maths 80 美女
001 张三 男 english 90 帅哥
002 赵四 女 maths 75 美女
002 赵四 女 english 95 帅哥
表名:Studentinfo:
studentid name sex
001 张三 男
002 赵四 女
表名:Coursescore
Studentid maths english
001 80 90
002 75 95
表名:Teacherinfo
teacherid tname course
101 美女 maths
102 帅哥 english想要的结果:请各位指导SQL如何写:
Studentid name sex course score tname
001 张三 男 maths 80 美女
001 张三 男 english 90 帅哥
002 赵四 女 maths 75 美女
002 赵四 女 english 95 帅哥
解决方案 »
- 很基础 的 ibeats 问题 isnotnull 与 isnotempty 的区别
- Oracle统计字符出现次数问题
- oracle:delete触发器怎么更新当前表
- 单列字符串合并成以逗号连接的一行
- 1:n关系,sql语句实现横向展示问题。感觉很有难度!!!
- 这个select语句是什么意思??
- 被捆扰的一个问题,在线求解!!
- 请问这个触发器有什么错误,是从sql server里面改成oracle的!
- 存储过程中如何判断一个变量为空?
- 求救:最简单的select * from table在sql*plus里都执行不起来。老是‘ORA-00942: 表或视图不存在’
- 新手求助,plsql,function,第一次写,不知道错在哪里
- 想得到某一天的最早时间点和最晚时间点
from Studentinfo m , Teacherinfo n , Coursescore o
where m.studentid = n.studentid and m.studentid = o.studentid
union all
select m.*,n.course , o.english score n.tname
from Studentinfo m , Teacherinfo n , Coursescore o
where m.studentid = n.studentid and m.studentid = o.studentid
FROM studentinfo A,coursescore b,teacherinfo c
where a.studentid=b.studentid and c.course='maths'
UNION ALL
SELECT A.*,c.course,b.english,c.tname
FROM studentinfo A,coursescore b,teacherinfo c
where a.studentid=b.studentid and c.course='english'这个Coursescore表结构设计得不合理..
insert into Studentinfo values('001', '张三', '男')
insert into Studentinfo values('002', '赵四', '女')
create table Coursescore(Studentid varchar(10),maths int,english int)
insert into Coursescore values('001', 80 ,90)
insert into Coursescore values('002', 75 ,95)
create table Teacherinfo(teacherid varchar(10),tname varchar(10),course varchar(10))
insert into Teacherinfo values('101', '美女', 'maths')
insert into Teacherinfo values('102', '帅哥', 'english')
goselect m.*,n.course , o.maths score ,n.tname
from Studentinfo m , Teacherinfo n , Coursescore o
where m.studentid = o.studentid and n.course = 'maths'
union all
select m.*,n.course , o.english score ,n.tname
from Studentinfo m , Teacherinfo n , Coursescore o
where m.studentid = o.studentid and n.course = 'english'
drop table Studentinfo , Coursescore ,Teacherinfo/*
studentid name sex course score tname
---------- ---------- ---------- ---------- ----------- ----------
001 张三 男 maths 80 美女
002 赵四 女 maths 75 美女
001 张三 男 english 90 帅哥
002 赵四 女 english 95 帅哥(所影响的行数为 4 行)*/
select '001' studentid,'张三' name,'男' sex from dual union
select '002' studentid,'赵四' name,'女' sex from dual),
Coursescore as(
select '001' studentid, 80 maths, 90 english from dual union
select '002' studentid, 75 maths, 95 english from dual),
Teacherinfo as(
select '101' teacherid,'美女' tname, 'maths' course from dual union
select '102' teacherid,'帅哥' tname, 'english' course from dual)SELECT s.Studentid, s.name, s.sex, c.course, c.score, t.tname
FROM Studentinfo s,
(SELECT studentid, 'maths' course, maths score
FROM Coursescore
UNION ALL
SELECT studentid, 'english' course, english score FROM Coursescore) c,
Teacherinfo t
WHERE s.studentid = c.studentid AND
c.course = t.course;
2 (select case when t.course = 'maths' then maths
3 when t.course = 'english' then english
4 else 0
5 end
6 from Coursescore c
7 where c.studentid = s.studentid
8 ) score,
9 t.tname
10 from Studentinfo s, Teacherinfo t
11 /
STUDENTID NAME SEX COURSE SCORE TNAME
---------- ---------- ---------- ---------- ---------- ----------
001 张三 男 maths 80 美女
001 张三 男 english 90 帅哥
002 赵四 女 maths 75 美女
002 赵四 女 english 95 帅哥