简体   繁体   中英

How to get data of last 7 days from last friday in mysql?

Im trying to get data of last 7 days starting from Friday. I tried almost every query but its not working. It returns only one day. Can someone help me? Here is my query.

SELECT DISTINCT ticketlogs.dcid, COUNT(*) AS `count` 
  FROM ticketlogs 
 WHERE dcid = $dcid
   AND DATE(ticketdate) = 
       DATE_FORMAT(DATE_SUB(NOW(),
                   INTERVAL ((7 + WEEKDAY(DATE_SUB(NOW(), INTERVAL 1 WEEK))
                                                         - 4) % 7) DAY), '%Y-%m-%d')

The previous Friday ends at:

(case when dayofweek(now()) = 7 then curdate() - interval 1 day
      when dayofweek(now()) = 6 then curdate()
      else dayofweek(now()) - interval dayofweek(now()) day
 end)

You can incorporate this into your logic. I would use:

select t.*
from ticketlogs tl cross join
     (select (case when dayofweek(now()) = 7 then curdate() - interval 1 day
                   when dayofweek(now()) = 6 then curdate()
                   else dayofweek(now()) - interval dayofweek(now()) day
              end) as saturday
     ) x
where tl.ticketdate < x.saturday and
      tl.ticketdate >= x.saturday - interval 7 day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM