Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:26:23+00:00 2026-06-08T13:26:23+00:00

We are using MySQL as our database to store messages with timestamps. Is it

  • 0

We are using MySQL as our database to store messages with timestamps. Is it possible to create a query that returns messages of the last n weekdays?

I.e. if n is 4 and today is Tuesday, I want messages from this weeks Monday, last weeks Friday, last weeks Thursday and last weeks Wednesday .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:26:25+00:00Added an answer on June 8, 2026 at 1:26 pm

    If you want to do this directly with mysql it would be a little complicated. As Vatev recommended you should calculate date star date in advance, but if you really want to do this, you’ll probably need following functions:

    • ADD_DATE, with INTERVAL -N WEEKS
    • FLOOR, in C int/int would do just fine
    • MOD, a % b 🙂
    • WEEKDAY

    First of all you need should count how many weeks you should go back, that’s easy… For you one week = 5 days, that means

    weeks = FLOOR(days / 5)
    

    We’ve taken care of weeks, so we’ll now have to work with the rest:

    rest = days MOD 5
    

    Now we have two cases, weekend has occurred or no, for the case that there wasn’t weekend days are good. We have to add 2 days to skip it. The weekend occurred if (WEEKDAY(now) - rest) < 0

    rest = IF( (WEEKDAY(now) - rest) < 0, rest + 2, rest)
    

    And now we can build it to one par (let’s assume you have {days} and {rest} pre-calculated):

    WHERE date >= ADD_DATE(
        ADD_DATE (
            {now},
            INTERVAL -IF( (WEEKDAY({now}) - {rest}) < 0, {rest} + 2, {rest}) DAYS, 
        ),
        INTERVAL -FLOOR({days} / 5) WEEKS
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using a mysql database w/ about 150,000 records (names) total. Our searches
We're considering using UUID values as primary keys for our MySQL database. The data
We have a product that uses a MySQL database as the data-store. The data-store
Using MySQL should I store time data as an Int which would be the
Using MySQL, How do I import a user defined database function from one db
Using MySQL, I have a simple table that logs the IP Address that users
Using mysql 5.5.2 on windows (Wamp) I'm writing a Java application that use several
(Using MySQL and PHP) I have a search form that will allow my users
Im using MySQL with PDO PHP scripts to maintain a user database and a
MySQL Query works fine using MySQL workbench but produces an error when I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.