mysql 查七天的支付

paylog表 date userid todayPay
现在我要查一个sevenDayPay

就是加上每个date每个uid的近七天支付之和,最好一个sql能搞定

阅读 3.2k
4 个回答
select *,(select sum(todayPay) from paylog where date between (pa.date- interval 7 day) and pa.date and uid=pa.uid) as subPay from paylog pa 

好像是这样的。 (谢谢rebiekong的提醒)

你需要的是一个函数subdate()

select sum(todayPay) as subPay from paylog where date >= unix_timestamp(DATE_SUB(curdate(),INTERVAL 6 DAY))  group by uid

当然了,如果你要配合php,可以计算出当前时间减去7天/6天 的时间戳,然后放入sql替换就可以了/

SELECT * FROM article WHERE date_sub(curdate(), INTERVAL 7 DAY) <= DATE(add_time);
可以参考PHP程序员雷雪松的博客:http://www.leixuesong.cn/2065

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题