我想检索上个月的数据和当月数据
我的查询正常工作
上个月数据计算从当前日期起1个月。但是我希望我的数据计算为calander month而不是我当前的日期。
喜欢
我目前的日期是2014-07-23
现在我的计算通过计算当前日期的月份来返回数据。
但我想基于日历jan,feb,march range。
我上个月的当前查询
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)
答案 0 :(得分:0)
上个月
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())