----昨天 select * from 表 where Time>=datetime('now','start of day','-1 day') and Time<datetime('now','start of day','+0 day') ----当天 select * from 表 where Time>=datetime('now','start of day','+0 day') and Time<datetime('now','start of day','+1 day') ----当周 select * from 表 where Time>=datetime('now','start of day','-7 day','weekday 1') AND Time<datetime('now','start of day','+0 day','weekday 1') ----当月 select * from 表 where Time>=datetime('now','start of month','+0 month','-0 day') AND Time < datetime('now','start of month','+1 month','0 day') ----上月 select * from 表 where Time>=datetime('now','start of month','-1 month','-0 day') AND Time <datetime('now','start of month','+0 month','-1 day')
上面的“当周”和“当月”的获取方法,知识取最近7(30)天,并非本周(本月)。
下面是经过测试可以用的方法:
--本月 select * from UserConsume where rowTime between datetime('now','start of month','+1 second') and datetime('now','start of month','+1 month','-1 second') --本周 select * from UserConsume where rowTime between datetime(date(datetime('now',strftime('-%w day','now'))),'+1 second') and datetime(date(datetime('now',(6 - strftime('%w day','now'))||' day','1 day')),'-1 second')
----上面本周的计算再次出错,再次修正:
--start
select datetime("now","start of day","-6 day","weekday 1")
--end
select datetime(datetime("now","start of day","weekday 0"),"start of day","+1 day")