2010-03-26 61 views
-1

我想从给定日期的最后1年的数据。例如:查询使用mysql从给定日期查找去年每个月的数据?

我有日期2013-06-01 ,我想数据如下也是数据我是从三个表使用Group By或别的东西

Month | Amount | Total_Data 
June 2013  100  5 
May 2013  80  4 
     -  100  5 
     -  100  5 
July 2012  10  2 
希望

我尝试下面的查询,但没有锻炼

SELECT DATE_FORMAT(rf.period, '%M %Y') as Month 
    , sum(p.amount * ((100-q.amount)/100)) as Amount 
    ,count(distinct q.label_id) as Total Data 
FROM table1 rf 
    , table2 p 
    , table3 q 
    ,table4 a 
where rf.period BETWEEN '2013-06-01' AND '2013-06-01' 
    and q.royalty_period BETWEEN '2013-06-01' AND '2013-06-01' 
    and a.id = q.album_id 
    and p.file_id = rf.id 
    and p.upc = a.upc 
    and p.local_revenue is not null 
GROUP BY Month 
+0

它是如何锻炼的?你的结果是什么? – 2010-03-26 07:10:10

回答

0

你有相同的日期,在您的BETWEEN子句:“2013年6月1日” ==“2013年6月1日” 所以它只有1天的时间

+0

请注意,在两个BETWEEN子句中都可以看到相同的语法。 – 2010-03-26 07:11:36

+0

是的,这只是一个例子,因为我正在尝试,但我想要我想要的实际数据是如上所述。 如果我将其更改为'2013-06-01'和'2012-07-01',它不会给我提供任何数据,因为我需要12个月的实际数据。 – Salil 2010-03-26 07:13:50

+0

@Salil你在这里混淆了BETWEEN的最小和最大参数http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#operator_between – 2010-03-26 07:24:58

相关问题