表1
课程号 课程名 授课老师
11 数学 刘老师
12 语文 张老师
13 政治 刘老师
14 化学 李老师
15 生物 肖老师
16 英语泛读 牛老师
表2学号 课程号 分数
1001 11 90
1002 12 80
1003 13 85
1004 14 90
1005 15 95
1005 14 90
1006 16 85
1005 16 60
1004 16 95
表3学号 姓名 班级
1001 李红 计算机
1002 张丽 金融
1003 凯东 外语
1004 黄忠 外语
1005 简单 计算机
1006 李聪 金融
1007 何洁 外语 检索选两门课程的班级
课程号 课程名 授课老师
11 数学 刘老师
12 语文 张老师
13 政治 刘老师
14 化学 李老师
15 生物 肖老师
16 英语泛读 牛老师
表2学号 课程号 分数
1001 11 90
1002 12 80
1003 13 85
1004 14 90
1005 15 95
1005 14 90
1006 16 85
1005 16 60
1004 16 95
表3学号 姓名 班级
1001 李红 计算机
1002 张丽 金融
1003 凯东 外语
1004 黄忠 外语
1005 简单 计算机
1006 李聪 金融
1007 何洁 外语 检索选两门课程的班级
解决方案 »
- 把表中有个字段的最大值所在的记录查询出来
- 能否为已有表设置分区函数,头疼!
- 删除表级完整性约束的问题
- *********************请问关于SQLSERVER 2005 EXPRESS的问题**********************
- 不能在同一个sql server 中建立两个不同名称但结构相似的数据库吗
- SQLServer2000数据库连接Oracle10g问题
- 原因: 未与信任 SQL Server 连接相关联。
- 在对SQLSERVER操作的时候突然断开网络连接,再次打开网络连接后出现的问题.
- 这个sql语句怎么写。
- sql 问题
- 这个思路的sql如何写
- 问个有关作用域的问题
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
create table [表1]([课程号] int,[课程名] varchar(8),[授课老师] varchar(6))
insert [表1]
select 11,'数学','刘老师' union all
select 12,'语文','张老师' union all
select 13,'政治','刘老师' union all
select 14,'化学','李老师' union all
select 15,'生物','肖老师' union all
select 16,'英语泛读','牛老师'
--> 测试数据:[表2]
if object_id('[表2]') is not null drop table [表2]
create table [表2]([学号] int,[课程号] int,[分数] int)
insert [表2]
select 1001,11,90 union all
select 1002,12,80 union all
select 1003,13,85 union all
select 1004,14,90 union all
select 1005,15,95 union all
select 1005,14,90 union all
select 1006,16,85 union all
select 1005,16,60 union all
select 1004,16,95
--> 测试数据:[表3]
if object_id('[表3]') is not null drop table [表3]
create table [表3]([学号] int,[姓名] varchar(4),[班级] varchar(6))
insert [表3]
select 1001,'李红','计算机' union all
select 1002,'张丽','金融' union all
select 1003,'凯东','外语' union all
select 1004,'黄忠','外语' union all
select 1005,'简单','计算机' union all
select 1006,'李聪','金融' union all
select 1007,'何洁','外语'select [班级] from(
select c.*,d.课程名 from (
select a.学号,a.姓名,a.班级,b.课程号 from
[表3] a left join [表2] b
on a.学号=b.学号)c
left join [表1] d on c.课程号=d.课程号)e
group by [班级] having count(distinct 课程号)=2
/*
班级
金融
*/
更改一下,没注意是至少两门:
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
create table [表1]([课程号] int,[课程名] varchar(8),[授课老师] varchar(6))
insert [表1]
select 11,'数学','刘老师' union all
select 12,'语文','张老师' union all
select 13,'政治','刘老师' union all
select 14,'化学','李老师' union all
select 15,'生物','肖老师' union all
select 16,'英语泛读','牛老师'
--> 测试数据:[表2]
if object_id('[表2]') is not null drop table [表2]
create table [表2]([学号] int,[课程号] int,[分数] int)
insert [表2]
select 1001,11,90 union all
select 1002,12,80 union all
select 1003,13,85 union all
select 1004,14,90 union all
select 1005,15,95 union all
select 1005,14,90 union all
select 1006,16,85 union all
select 1005,16,60 union all
select 1004,16,95
--> 测试数据:[表3]
if object_id('[表3]') is not null drop table [表3]
create table [表3]([学号] int,[姓名] varchar(4),[班级] varchar(6))
insert [表3]
select 1001,'李红','计算机' union all
select 1002,'张丽','金融' union all
select 1003,'凯东','外语' union all
select 1004,'黄忠','外语' union all
select 1005,'简单','计算机' union all
select 1006,'李聪','金融' union all
select 1007,'何洁','外语'select [班级] from(
select c.*,d.课程名 from (
select a.学号,a.姓名,a.班级,b.课程号 from
[表3] a left join [表2] b
on a.学号=b.学号)c
left join [表1] d on c.课程号=d.课程号)e
group by [班级] having count(distinct 课程号)>=2
/*
班级
计算机
金融
外语
*/