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 8387195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:08:48+00:00 2026-06-09T18:08:48+00:00

I want to fetch the N previous weeks of data from my MySQL-table based

  • 0

I want to fetch the N previous weeks of data from my MySQL-table based on the date that’s in the table’s rows.

Imagine that the current week is number 30, and I have rows with data from week 29, 28, 26 and 12.

If I want the last two weeks rows based on todays date, that’s easy, but I want it based on the timestamp on the row.

So if N = 2, I want all rows from week 29 and 28. If N = 3 I want week 29, 28 and 26, no matter which week it is when I’m fetching it.

I could do this using PHP and guessing based on estimated output, but maybe there is some MySQL-functionality I can take advantage of.

  • 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-09T18:08:50+00:00Added an answer on June 9, 2026 at 6:08 pm

    One approach is to get the set of weeks you want returned using a query, and then using that query as an inline view which you can join to.

    Something like this:

    SELECT t.*
      FROM (SELECT s.week FROM tab s GROUP BY s.week ORDER BY s.week DESC LIMIT 3) r
      JOIN tab t
        ON t.week = r.week
    

    The inline view (aliased as r) gets a the list of the N latest week values in the table. (This assumes that the week column is sortable, like it would be if it were an integer.

    That is joined to the table, with the ON clause restricting the rows returned from the table to those rows that have a week value that matches one returned by the inline view.

    NOTE: an index on the week column may provide significant performance benefit for a large number of rows, and a high cardinality on the week column.

    NOTE: With large tables, there can be performance differences between doing a JOIN operation vs using an IN (subquery) predicate, as the example below. For most large sets, and the queries that I do, I find that the JOIN usually performs better. But you might want to test both, to see which performs better for you. (On small tables, it’s not going to matter much either way; the performance differences become significant on really big tables.)

    SELECT t.*
      FROM tab t
     WHERE t.week
        IN (SELECT s.week FROM tab s GROUP BY s.week ORDER BY s.week DESC LIMIT 3) r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this previous question on stackoverflow: Fetch Oracle table type from stored procedure
I have to filter data for last 3 months from current date, so that
I want to fetch the title from book table and the subject name from
I want to fetch the unmatching records from two table in SQL, the table
When I fetch a specific record from db table, I want to fetch the
want to fetch the value from dynamically created textfield with tag from NSMutableArray.... Txt_New_Estimated
i Want to fetch this data using json decode. Please Help.. So far i
I'm storing a string in mysql with the ENCRYPT() function. I want to fetch
I have a view controller that is used to populate data from the core
Possible Duplicate: How to sort by previous date in database? I want to do

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.