我刚刚学习数据库,联系到这一步的时候做不出来了!student(主键Sno)Sno Sname Ssex Sage 所在系
Sdept
95001 李勇 男 20 CS
95002 刘晨 女 21 IS
95003 王敏 女 18 MA
95004 张力 男 19 IS
Course表(主键Cno)
Cno 课程名Cname 先行课Cpno 学分Ccredit
1 数据库 5 4
2 数学 2
3 信息系统 1 4
4 操作系统 6 3
5 数据结构 7 4
6 数据处理 2
7 PASCAL语言 6 4
SC(主键Sno,Cno,外部键Sno,Cno)
SC表
Sno Cno Grade
95001 1 92
95001 2 85
95001 3 88
95002 2 90
95002 3 85
95003 3 59
[size=24px][size=16px]建立视图显示所有学生的数学成绩,显示字段有:学号、姓名、成绩。如果某个学生选择了数学课程则显示相应成绩;如果某个学生没有选择数学课程,则显示其学号和姓名,在成绩上显示空白。[/size][/size]
Sdept
95001 李勇 男 20 CS
95002 刘晨 女 21 IS
95003 王敏 女 18 MA
95004 张力 男 19 IS
Course表(主键Cno)
Cno 课程名Cname 先行课Cpno 学分Ccredit
1 数据库 5 4
2 数学 2
3 信息系统 1 4
4 操作系统 6 3
5 数据结构 7 4
6 数据处理 2
7 PASCAL语言 6 4
SC(主键Sno,Cno,外部键Sno,Cno)
SC表
Sno Cno Grade
95001 1 92
95001 2 85
95001 3 88
95002 2 90
95002 3 85
95003 3 59
[size=24px][size=16px]建立视图显示所有学生的数学成绩,显示字段有:学号、姓名、成绩。如果某个学生选择了数学课程则显示相应成绩;如果某个学生没有选择数学课程,则显示其学号和姓名,在成绩上显示空白。[/size][/size]
select 学号=s.sno,姓名=s.sname,成绩=grade
from student s
left join sc c on s.sno=c.sno
left join course r on r.cno=c.cno
where r.cname='数学'
create view vTest
as
select a.Sno,a.Sname,c.Grade
from student a
left join SC c
on a.Sno=c.Sno
left join Course
on b.Cno=c.Cno
where b.Cname='数学'
as
select a.Sno,a.Sname,c.Grade
from student a
left join SC c
on a.Sno=c.Sno
left join Course b
on b.Cno=c.Cno
where b.Cname='数学'
a.sno as 学号,a.aname as 姓名,b.grade as 成绩
from
student a
left join sc b on
a.sno=b.sno
left join course c on
b.cno=c.cno
where
b.cname='数学'
a.sno as 学号,a.aname as 姓名,isnull(b.grade,0) as 成绩
from
student a
left join sc b on
a.sno=b.sno
left join course c on
b.cno=c.cno
where
b.cname='数学'
无法绑定由多个部分组成的标识符 "b.Cno"。
消息 4104,级别 16,状态 1,过程 vTest,第 3 行
无法绑定由多个部分组成的标识符 "b.Cname"。
这是什么意思?
晕,那你填充的时候,不用b.Cname
加点点
create table #student(Sno int, Sname varchar(10),Ssex varchar(10),Sage int,Sdept varchar(10))
insert #student select
95001, '李勇', '男', 20 ,'CS' union all select
95002, '刘晨', '女', 21 ,'IS' union all select
95003, '王敏', '女', 18 ,'MA' union all select
95004, '张力', '男', 19 ,'IS' create table #Course(Cno int, Cname varchar(10),Cpno int,Ccredit int)
insert #Course select
1, '数据库' , 5 , 4 union all select
2, '数学' , null, 2 union all select
3, '信息系统' , 1 , 4 union all select
4, '操作系统' , 6 , 3 union all select
5, '数据结构' , 7 , 4 union all select
6, '数据处理' , null , 2 union all select
7, 'PASCAL语言', 6 , 4 create table #sc(sno int, Cno int,grade int)
insert #sc select
95001, 1 ,92 union all select
95001, 2 ,85 union all select
95001, 3 ,88 union all select
95002, 2 ,90 union all select
95002, 3 ,85 union all select
95003, 3 ,59
select 学号=s.sno,姓名=s.sname,成绩=grade
from #student s
left join (
select sno,grade from #sc c
join #course r on r.cno=c.cno
where r.cname='数学'
)t
on s.sno=t.sno
学号 姓名 成绩
----------- ---------- -----------
95001 李勇 85
95002 刘晨 90
95003 王敏 NULL
95004 张力 NULL(4 行受影响)
drop table #student
drop table #sc
drop table #course
create table student
(
sno varchar(8) primary key,
sname varchar(8),
ssex varchar(4),
sage varchar(8)
)
go
create table course
(
cno int primary key,
cname varchar(16),
cpno varchar(8),
ccredit int
)
go
create table sc
(
sno varchar(8),
cno int,
grade int
constraint pk_scno primary key(sno,cno)
)alter table sc
add constraint fk_sno foreign key (sno) references student (sno),
constraint fk_cno foreign key (cno) references course (cno)insert into student
select '95001','李勇','男',20 union all
select '95002','刘晨','女',21 union all
select '95003','王敏','女',18 union all
select '95004','张力','男',19
goinsert into course
select 1,'数据库',5,4 union all
select 2,'数学',null,2 union all
select 3,'信息系统', 1,4
go insert into sc
select '95001',1,92 union all
select '95001',2,85 union all
select '95001',3,88 union all
select '95002',2,90 union all
select '95002',3,85 union all
select '95003',3,59 select s.sno,s.sname,scc.grade from student s left join sc scc on (scc.sno=s.sno and scc.cno=(select cno from course where cname='数学'))
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-29 09:56:18
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[student]
if object_id('[student]') is not null drop table [student]
go
create table [student]([Sno] varchar(5),[Sname] varchar(4),[Ssex] varchar(2),[Sage] int,[Sdept] varchar(2))
insert [student]
select '95001','李勇','男',20,'CS' union all
select '95002','刘晨','女',21,'IS' union all
select '95003','王敏','女',18,'MA' union all
select '95004','张力','男',19,'IS'
--> 测试数据:[Course]
if object_id('[Course]') is not null drop table [Course]
go
create table [Course]([Cno] int,[Cname] varchar(10),[Cpno] int,[Ccredit] int)
insert [Course]
select 1,'数据库',5,4 union all
select 2,'数学',2,null union all
select 3,'信息系统',1,4 union all
select 4,'操作系统',6,3 union all
select 5,'数据结构',7,4 union all
select 6,'数据处理',2,null union all
select 7,'PASCAL语言',6,4
--> 测试数据:[SC]
if object_id('[SC]') is not null drop table [SC]
go
create table [SC]([Sno] int,[Cno] int,[Grade] int)
insert [SC]
select 95001,1,92 union all
select 95001,2,85 union all
select 95001,3,88 union all
select 95002,2,90 union all
select 95002,3,85 union all
select 95003,3,59
--------------开始查询--------------------------
select
isnull(a.sno,b.sno) as 学号,isnull(a.sname,'') as 姓名,isnull(ltrim(b.grade),'') as 成绩
from
student a
left join
(select c.sno,c.Grade from [Course] b left join [SC] c on b.cno=c.cno where b.cname='数学')b
on
a.sno=b.sno
----------------结果----------------------------
/*学号 姓名 成绩
----- ---- ------------
95001 李勇 85
95002 刘晨 90
95003 王敏
95004 张力 (4 行受影响)
*/