一道查询题目请帮忙下

ping79857938 2010-03-02 08:24:07

S
S# SNAME AGE SEX
1 李强 23 男
2 刘丽 22 女
5 张友 22 男

C
C# CNAME TEACHER
k1 C语言 王华
k5 数据库原理 程军
k8 编译原理 程军

SC
S# C# GRADE
1 k1 83
2 k1 85
5 k1 92
2 k5 90
5 k5 84
5 k8 80

问:检索全部学生都选修的课程的课程号(C#)和课程名(CNAME)。
...全文
72 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
s85118629 2010-03-02
  • 打赏
  • 举报
回复
引用 1 楼 jwdream2008 的回复:
SQL codeselectdistinct C#,CNAMEFrom Cwhere C#In (select C#from SCgroupBY C#having(count(C#)=(selectcount(1)from S)))

distinct 不用吧
百年树人 2010-03-02
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[S]') is not null drop table [S]
go
create table [S]([S#] int,[SNAME] varchar(4),[AGE] int,[SEX] varchar(2))
insert [S]
select 1,'李强',23,'男' union all
select 2,'刘丽',22,'女' union all
select 5,'张友',22,'男'
if object_id('[C]') is not null drop table [C]
go
create table [C]([C#] varchar(2),[CNAME] varchar(10),[TEACHER] varchar(4))
insert [C]
select 'k1','C语言','王华' union all
select 'k5','数据库原理','程军' union all
select 'k8','编译原理','程军'
if object_id('[SC]') is not null drop table [SC]
go
create table [SC]([S#] int,[C#] varchar(2),[GRADE] int)
insert [SC]
select 1,'k1',83 union all
select 2,'k1',85 union all
select 5,'k1',92 union all
select 2,'k5',90 union all
select 5,'k5',84 union all
select 5,'k8',80

---查询---
select C#,Cname
from C
where not exists
(
select 1
from s
where not exists
(
select 1
from sc
where s.s#=sc.s# and c.c#=sc.c#
)
)

---结果---
C# Cname
---- ----------
k1 C语言

(所影响的行数为 1 行)
jwdream2008 2010-03-02
  • 打赏
  • 举报
回复
select distinct C#,CNAME From C where C# In (select C# from SC group BY C# having(count(C#)=(select count(1) from S)))

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧