一张表table 拥有主键id,姓名name,时间字段date,成绩achievement;怎样使用一条sql语句查询出本月的,上月的以及去年本月3个时间每个人的成绩
用的是mysql
怎样用1条sql查询出3个时间的数据
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
Qshu 2018-11-06 08:40关注with a as (),
b as (),
c as ()
select 姓名,a.成绩 今年本月,b.成绩 今年上月 ,c.成绩 去年本月 from a,b,c
本年本月
substr(sysdate,8,2) year
substr(sysdate,4,2) month今年上月
substr(sysdate,8,2) year
substr(sysdate,4,2) month-1去年本月
substr(sysdate,8,2) year-1
substr(sysdate,4,2) montha,b,c查询姓名成绩 where date的年等于年,月等于月
全部SQL如下:
with a as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2) and date的月=substr(sysdate,4,2)),b as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2) and date的月=substr(sysdate,4,2)-1),
c as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2)-1 and date的月=substr(sysdate,4,2))
select 姓名,a.成绩 今年本月,b.成绩 今年上月 ,c.成绩 去年本月 from a,b,c
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报