做了一个自动加分的,但是出现了不是我想要的结果。
关键在这一句话if(select avg(bis) from chengji)>=97 如果按照我运行后的结果,我不理解为什么。
请大侠们指点迷津--学生成绩use test
if exists(select * from sysobjects where name='chengji')
drop table chengjicreate table chengji(
stuid int not null,
jis float,
bis float
)if exists(select * from sysobjects where name='student')
drop table student create table student(
stuid int identity(1,1) not null,
stuname varchar(20) not null
)alter table student
add constraint PR_id primary key(stuid)alter table chengji
add constraint FK_sid 
foreign key(stuid) references student(stuid)go
insert into student values('张秋丽')
insert into student values('李斯文')
insert into student values('李文才')
insert into student values('欧阳俊兄')
insert into student values('梅超风')insert into chengji values(1,56,62);
insert into chengji values(2,70,85);
insert into chengji values(3,94,55);
insert into chengji values(4,88,77);go
--没有参加考试的
select stuname from student where stuid not in(select stuid from chengji)
--用连接语句把两个表合成一个
select * from student as s left join chengji as c on s.stuid=c.stuid
go
--用子查询查看实到人数,等信息
select count(s.stuid)as '应到人数' ,
(select count(c.stuid) from chengji as c)as '实到人数' ,
(select count(*) from student as ss where ss.stuid not in(select cc.stuid from chengji as cc))as '缺考人数'
from student as s select stuname as '姓名'
,'机试'=case
when jis is null then '缺考'
else convert(varchar(6),jis)
end
,'笔试'=case
when bis is null then '缺考'
else convert(varchar(6),bis)
end
,'通过'=case
when jis>60 and bis>60 then '是'
else '否'
end
  from student as s left join chengji as c on s.stuid=c.stuid 
go
declare @jis int,@bis int
select @jis=avg(jis) from chengji
select @bis=avg(bis) from chengji
print @jis;
print @bis;
if(@jis>@bis)
begin
while(1=1)
begin 
update chengji set bis=bis+2
if(select avg(bis) from chengji)>=97
break
end
end
else
while(1=1)
begin 
update chengji set jis=jis+2
if(select avg(jis) from chengji)>=97
break
end
---加分后---
select stuname as '姓名'
,'机试'=case
when jis is null then '缺考'
else convert(varchar(6),jis)
end
,'笔试'=case
when bis is null then '缺考'
else convert(varchar(6),bis)
end
,'通过'=case
when jis>60 and bis>60 then '是'
else '否'
end
  from student as s left join chengji as c on s.stuid=c.stuid 运行后的最后一个的打印结果如下:张秋丽 56 90 否
李斯文 70 113 是
李文才 94 83 是
欧阳俊兄 88 105 是
梅超风 缺考 缺考 否

解决方案 »

  1.   


    declare @jis int,@bis int 
    select @jis=avg(jis) from chengji 
    select @bis=avg(bis) from chengji 
    print @jis; 
    print @bis; 
    --楼主,if(@jis>@bis)后的语句能不能就用下面这两条句子代替呢?update chengji set bis=bis+2 where @jis>@bis and @jis<97
    update chengji set jis=jis+2 where @jis<=@bis and @bis<97
    --结果
    张秋丽 56 64 否
    李斯文 70 87 是
    李文才 94 57 否
    欧阳俊兄 88 79 是
    梅超风 缺考 缺考 否