我想检索上个月的数据和当月的数据
我的查询正常工作
上个月的数据从当前日期算起1个月。但是我希望我的数据不是从当前日期起算为日历月。
喜欢
我当前的日期是2014-07-23
现在,我的计算通过计算从当前日期开始的月份返回数据。
但我想基于日历1月,2月,3月范围。
我当前查询的上个月
SELECT (100*AVG((m.carbs)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_carbs,(100*AVG((m.fat)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_fat,(100*AVG((m.protein)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_protein
FROM `meal` AS m,`user_history` as u where u.meal_id=m.id and u.user_id=$user_id and date(FROM_UNIXTIME(u.create)) BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
我本月的当前查询
SELECT (100*AVG((m.carbs)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_carbs,(100*AVG((m.fat)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_fat,(100*AVG((m.protein)/((m.carbs)+(m.fat)+(m.protein)))) AS Percantage_protein
FROM `meal` AS m,`user_history` as u where u.meal_id=m.id and u.user_id=$user_id and YEARWEEK(date(FROM_UNIXTIME(u.create))) = YEARWEEK(CURRENT_DATE)
最佳答案
上个月
year(date(FROM_UNIXTIME(u.create))) = year(CURDATE() - INTERVAL 1 MONTH)
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE() - INTERVAL 1 MONTH)
这个月
year(date(FROM_UNIXTIME(u.create))) = year(CURDATE())
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE())