注册 登录
编程论坛 SQL Server论坛

关于查询的

静思 发布于 2007-05-23 09:44, 702 次点击

create table student--创建学生表
(Sno char(8) primary key,
Sname char(20) unique,
Sage int
constraint check_age check (sage between 14 and 35),--建立约束
Sdept char(6));

create table course--创建课程表
(Cno char(5) primary key,
Cname char (10),
Ctime int ,
Ccredit char(2),
Cpno char(5)
foreign key (Cpno) references course (Cno));--参照完整性

create table sc--创建选课表
(Sno char(8),
Cno char(5),
Grade int,
primary key (Sno,Cno),
foreign key (Sno) references student (Sno),
foreign key (Cno) references course(Cno));

以上是我创建的三个表,有关表中内容详见附件。
我想查询一下计算机专业学生的选课信息,但有些计算机系的学生由于没有选课,所以在选课表中没有记录。
例如我想查询计算机系每个学生的姓名,选修的课程名和成绩(没有选课的也要列出)
如果使用下列查询语句只能列出选课的学生信息:
select sname '姓名',cname'课程名',grade'成绩'
from student stu,course co,sc
where sdept='计算机'and stu.sno=sc.sno and co.cno=sc.cno


有哪位高手知道怎么写查询语句才能列出计算机系每个学生的姓名,选修的课程名和成绩(没有选课的也要列出)
请指教,不胜感激!~!◎!~!


只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-5-23 9:52:16编辑过]

6 回复
#2
bygg2007-05-23 09:47
full join
#3
cyyu_ryh2007-05-23 09:51

是否可添加个子查询,来查询未选课程的

#4
静思2007-05-23 10:01

SQL SERVER 2000 中的外连接怎么表示?我用下列查询语句有错误:
select stu.sno '学号',sname'姓名',sage'年龄',sdept'院系',Cno,Grade
from student stu,sc
where stu.sno=sc.sno(*) and sdept='计算机';

#5
Kendy1234562007-05-23 11:01
select sname '姓名',cname '课程名',grade '成绩'
from student stu left join sc s on stu.sno=s.sno
left join course co on co.cno=sc.cno
where sdept='计算机'
#6
初学Delphi2007-05-23 11:14
以下是引用静思在2007-5-23 10:01:08的发言:

SQL SERVER 2000 中的外连接怎么表示?我用下列查询语句有错误:
select stu.sno '学号',sname'姓名',sage'年龄',sdept'院系',Cno,Grade
from student stu,sc
where stu.sno=sc.sno(*) and sdept='计算机';

你这个相当于
inner join 了
按照楼上的方法
left join
right join
full join

#7
静思2007-05-23 11:28
已解决,谢谢各位的回答!~!
1