表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       何洁       外语      检索选两门课程的班级

解决方案 »

  1.   


    --> 测试数据:[表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
    /*
    班级
    金融
    */
      

  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
    /*
    班级
    计算机
    金融
    外语
    */