欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

复杂的根据不同的情况 查学生的分数 (条件不同 算的比也不同) 自己亲自做的 有大用 有大用

shiping1 的头像

 学生分数 在 2015年5月1日 之前是 score/1.2

在 2015年5月1日之后 是 score/1.5 
select sum(score)/1.2 as sum1 from student 
where name="学生姓名" and date<='2015-05-01' 
select sum(score)/1.5 as sum2  from student 
where name="学生姓名" and date>'2015-05-01'
然后 sum1+sum2 
请问这个功能 有没有一个 sql 语句来实现的  
学生本来 是 120分 的试卷 后来 改成 150分的试卷了 这里的目的是把 120分折算成 100分,即 /1.2 把 150分折算成 100,即 /1.5

1)一种方法
 select sum(case when date<='2015-05-01' then score/1.2 else score/1.5 end) from student where name = '学生姓名'; 

2)二种方法 


 date关键字要加反单, 时间格式比较一般都是unixtimestamp 

3) 三种方法 

 select ((select sum(score)/1.2 as sum1 from student where name="学生姓名" and date<='2015-05-01') + 
(select sum(score)/1.5 as sum2  from student where name="学生姓名" and date>'2015-05-01')) as total ;    
----这样应该也行的 

4)四种方法


 

5)五种方法 
select t1.sum1+t2.sum2 from 
(select sum(score)/1.2 as sum1 from student 
where name="学生姓名" and date<='2015-05-01') as t1,
(select sum(score)/1.5 as sum2  from student 
where name="学生姓名" and date>'2015-05-01') as t2


普通分类: