mysql - SQL select current week/last week -


i'm looking way of selecting rows based on:

  • current week (s-s)
  • previous week (s-s)

the problem i'm having selecting sunday-sunday.

at moment i'm using:

select sum(time)  `time`  `projectid` = '$pid' && created > date_sub(now(), interval 1 week) 

any great, thanks!

mysql have week function can use:

select sum(time)  `time`      `projectid` = '$pid'     , created > now() - interval 2 week     , week(created) in (week(now()), week(now() - interval 1 week)) 

notes

the first condition (created > now() - interval 2 week) needed data of current , previous weeks first , restrict 2 weeks interested in. otherwise, if had enough data, aggregation of data of corresponding weeks of every year in table. has added benefit of allowing query use index on field.

you need use "week(now() - interval 1 week)" due first week of year. otherwise, week(now()) - 1 have sufficed


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -