Http://hi.baidu.com/liangjian1024/blog/item/6861541b1416094842a9ad7c.html
Table finance has two fields as follows
Date Date
Money Double (15,2)
The following need for the 2010 financial data of the table finance, monthly statistics
SQL code
- Select Date_format (date,'%y-%m ') as month,sum(Money) as Money
- from Finance
- where Date_format (date,'%Y ') =2010
- Group by Month
- Order by Month
Here are the weekly statistics
View MySQL's Manual
%x year for the week where Sunday are the first day of the week, numeric, and four digits; Used with%V
%x year for the week, where Monday are the first day of the week, numeric, and four digits; Used with%v
which
1 if Monday is the first day of the week, then (lowercase) date_format (DATE, '%x%v ')
2 If Sunday is the first day of the week, then (uppercase) Date_format (DATE, '%x%V ')
SQL code
- Select Date_format (date,'%x year-%v Week ') as week,sum(Money) as money
- from Finance_base
- where Date_format (date,'%Y ') =2010
- Group by week
MySQL Monthly by week statistics