数据统计

ps:时间字段为时间戳

1select DATE_FORMAT(from_unixtime(datetime),'%Y-%m-%d') days,sum(amount) as count,count(id) as num from 表名 WHERE date_format(from_UNIXTIME(`datetime`),'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') group by days ORDER BY days DESC //当天

2SELECT * FROM 表名 WHERE TO_DAYS(date_format(now(),'%Y-%m-%d'))-TO_DAYS(date_format(from_UNIXTIME(`datetime`),'%Y-%m-%d')) = 1 //昨天
3SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY)<= date(date_format(from_UNIXTIME(`datetime`),'%Y-%m-%d'))  //近7天

 时间字段为datetime

SELECT * FROM 表名 DATE_SUB(CURDATE(), INTERVAL 2 DAY) >= date_format(`时间字段`,'%Y-%m-%d');//2天前的数据

 

posted @ 2017-08-21 09:32  pengcx  阅读(355)  评论(0编辑  收藏  举报