MySQL date between last monday and the next sunday -


i'm looking find records have booking date between previous monday , next sunday in mysql.

so far have:

select firstname      , lastname      , sessions      , (select count(memberid)            bookings          m.memberid = b.memberid               , b.date between lastmonday , nextsunday) sessionsused    members 

i'm looking substitute lastmonday , nextsunday

any appreciated!

mysql's yearweek() function selects unique value each week can use comparison. takes a second parameter specifies whether weeks start on sunday (0) or monday (1).

select count(memberid)  bookings m.memberid = b.memberid   , yearweek(b.date, 1) = yearweek(now(), 1); 

this select rows b.date in current week. specific week in past, change now() whatever date expression require.

for more generic case week not start on sunday or monday, need more complicated logic. here substitute @weekday day on weeks begin, 2 = tues, 3 = wed, 4 = thu, 5 = fri, 6 = sat.

select count(memberid)  bookings m.memberid = b.memberid   , date(b.date)     between date_sub(date(now()), interval (weekday(now()) - @weekday + 7) % 7 day)         , date_add(date(now()), interval 6 - (weekday(now()) - @weekday + 7) % 7 day); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -