这段sql怎么写

来源:百度知道 编辑:UC知道 时间:2024/09/23 16:30:42
数据库表sztabl中有字段
字段 szname date je
值 工资 2008-8-1 1000
工资 2008-7-1 1000
工资 2008-8-11 1000
我想通过sql查询后显示成
名称 7月 8月 总额
工资 1000 2000 3000
这个怎么写sql
是用sql输出到页面
确切的说我需要asp代码

select szname as '名称',(select top 1 sum(szmoney) from sz group by month(szdate))as '7月',
(select top 1 sum(szmoney) from sz group by month(szdate) order by month(szdate) desc)as '8月',
sum(szmoney)as '总额'
from sz group by szname
============
你看看吧,可能字段不太一样,不过内容都一样的~~~

先用strtotime转化日期为时间戳 定义为$date
7月总额:
select sum from sztabl where 1 between 7月1日0000的时间戳 and 7月31日2400的时间戳
8月总额:
select sum from sztabl where 1 between 8月1日0000的时间戳 and 8月31日2400的时间戳
总额:
select sum from sztabl

先做一个虚拟表
工资 7月 8月 总额
然后统计7月工资和插入进对应列,再插入8月数据,最后累加7月和8月的数值。