查询最低分大于70,最高分小于90的学生的学号
学号              课程表            分数
101        2-201      90
101        3-105      64
101        6-166      85
103        3-105      92
103        3-245      86
105        3-105      88
105        3-245      45
107        3-105      91
107        3-245      NULL
107        6-106      79
107        6-166      93
108        3-105      78
108        6-166      81
109        3-105      76
109        3-245      54
110        9-888      71
111        3-105      95我这样写怎么会有两个108  晕
select a.学号 from 成绩表 a
,(select 学号,max(分数) as max from 成绩表 group by 学号)b
,(select 学号,min(分数) as min from 成绩表 group by 学号)c
where 
a.学号=b.学号 and a.学号=c.学号 and
b.max<90 and c.min>70

解决方案 »

  1.   

    select a.学号 from 成绩表 a
    ,(select 学号,max(分数) as max,MIN(分数) as min from 成绩表 group by 学号 having max(分数)<90 and min(分数)>70)bwhere 
    a.学号=b.学号这样写  也还是有两个108
      

  2.   

    select 学号,max(分数) as max,MIN(分数) as min from 成绩表 group by 学号 having max(分数)<90 and min(分数)>70这样写 就只有108  110但题意是只要学号
      

  3.   

    select
      学号
    from  
      成绩表 a
    group by
      学号
    having
      max(分数)<90 and min(分数)>70
      

  4.   

    你那个球出的是所有学号 并没有分组 所以还需要distinct 一下才可以
      

  5.   

    哎,还是没意会到sql查询的精华啊
    完全和其他C语言之类的不一样,晕死哦
      

  6.   


    if object_id('tb','u') is not null
       drop table tb
    go
    create table tb
    (
     学号 int,
     课程表 varchar(10),
     分数 int
    )
    go
    insert into tb
    select 101,'2-201',90 union all
    select 101,'3-105',64 union all
    select 101,'6-166',85 union all
    select 103,'3-105',92 union all
    select 103,'3-245',86 union all
    select 105,'3-105',88 union all
    select 105,'3-245',45 union all
    select 107,'3-105',91 union all
    select 107,'3-245',null union all
    select 107,'6-106',79 union all
    select 107,'6-166',93 union all
    select 108,'3-105',78 union all
    select 108,'6-166',81 union all
    select 109,'3-105',76 union all
    select 109,'3-245',54 union all
    select 110,'9-888',71 union all
    select 111,'3-105',95
    go
    select 学号 from tb group by 学号 having min(分数)>70 and max(分数)<90
    go
    /*
    学号
    -----------
    108
    110
    警告: 聚合或其他 SET 操作消除了空值。(2 行受影响)*/
      

  7.   

    select
      学号
    from  
      成绩表 a
    where 
     
     分数<90 and 分数>70
      

  8.   

    select 学号 from   成绩表 where 分数<90 and 分数>70
      

  9.   

    select
      学号
    from  
      成绩表 a
    group by
      学号
    having
      max(分数)<90 and min(分数)>70