MySQL DATE_ADD usage, 5 day interval

前端 未结 3 514
半阙折子戏
半阙折子戏 2021-01-17 10:07

I\'m trying to select the order total sum ($) and invoice count over a 5 day period in a single query. I can\'t seem to get this to happen though. The current query I have

相关标签:
3条回答
  • 2021-01-17 10:39

    If your created column type is int then just try this one

    created<= UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 5 DAY))')
    
    0 讨论(0)
  • To get records between a date span, use:

    WHERE created BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)
    

    This example will get you records (assuming any exist for today, including time) for between today and days into the future. Look at DATE_SUB if you want to go into the past.

    0 讨论(0)
  • 2021-01-17 10:57
    WHERE created <= NOW() AND created >=NOW() - INTERVAL 5 DAY
    

    or it would be better to compare just DATE part of datetime:

    WHERE DATE(created) <= date(NOW()) AND 
      DATE(created) >= DATE(NOW() - INTERVAL 5 DAY)
    
    0 讨论(0)
提交回复
热议问题