首页 > mysql中怎么写存储过程求某年对应个月的业绩列表?

mysql中怎么写存储过程求某年对应个月的业绩列表?

比如现有数据库: test
表名: store
表结构:

g_id | g_date     | g_count |
a0001| 2014-01-01 | 1688    |
a0003| 2015-02-06 | 4680    |
a0003| 2015-02-08 | 1688    |
a0004| 2015-08-16 | 5688    | 

现在我想传入一个(年份)参数,比如2015,得到数据集:

月份    业绩
02      6368
08      5688

希望得到帮助,非常感谢!


create PROCEDURE getrecord(@year varchar(20))
AS
select convert(varchar(10),g_date,120) '月份',sum(g_count) '业绩' 
from test.store where convert(varchar(10),g_date,120)=@year
group by convert(varchar(10),g_date,120)

这是SQL Server的,不知道MySQL能不能用的起来 -。-


select 
date_format(g_date, '%m') as month,
sum(g_count) as count
from store where g_date>'2015' and g_date<'2016'
group by date_format(g_date, '%m')

你自己想個辦法弄出個 2016 就行了.
2015 默認是 2015-01-01 00:00:00.0
2016 默認是 2016-01-01 00:00:00.0

似乎只是 mysql 能這麼用, 其他數據庫得寫全 2015-01-01 這樣(這句話未進驗證).

【热门文章】
【热门文章】