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

  • Home
  • SEARCH
  • 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 7159961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:19:06+00:00 2026-05-28T13:19:06+00:00

My table structure is as follows: mid | tid | data | 1 |

  • 0

My table structure is as follows:

      mid   | tid  | data
    | 1     | 0    |  data
    | 1     | 1    |  data
    | 1     | 2    |  data
    | 1     | 3    |  data
    | 1     | 4    |  data
    | 2     | 0    |  data
    | 2     | 1    |  data
    | 3     | 0    |  data
    | 3     | 1    |  data
    | 3     | 2    |  data
    | 4     | 0    |  data
              .
              .
              .
    | N     | 0    |  data
    | N     | 1    |  data
    | N     | 2    |  data
              .
              .
    | N     | K    |  data

Each record is identified by 2 indices, a mid and tid. The tid will be unique to each mid. For each mid the tid starts at 0 and counts upwards to K. There can be imminently many tids for a given mid, and there can be infinitely many mids.

What I want to do is select the 3 most recent MIDs and their corresponding most recent tids WHERE the data meets some criteria (say > 1). If only MIDs 1, 2, 3, and 4 met the data requirements I would want it to return (omitting 1 since it is not the 3 most recent but rather the 4th):

    | 2     | 1    |  data
    | 3     | 2    |  data
    | 4     | 0    |  data

I am lost on how to do this, any help would be appreciated. A dual order by LIMIT 3 would return

    | 3     | 1    |  data
    | 3     | 2    |  data
    | 4     | 0    |  data
  • 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-05-28T13:19:07+00:00Added an answer on May 28, 2026 at 1:19 pm

    JOIN against a subquery which gets the MAX(tid) for each mid:

    SELECT 
      t.mid,
      t.tid,
      t.data
    FROM tbl t JOIN (
      SELECT mid, MAX(tid) AS maxtid FROM tbl GROUP BY mid
    ) m ON t.mid = m.mid AND t.tid = m.tid
    /* WHERE (some condition) */
    ORDER BY t.mid DESC
    LIMIT 3
    

    Update: Added the ORDER BY & LIMIT clause, which I missed in the OP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hopefully somebody can help The table structure is as follows: tblCompany: compID compName tblOffice:
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
My table structure is as follows: Person 1-M PesonAddress Person 1-M PesonPhone Person 1-M
The current structure is as follows: Table RowType: RowTypeID Table RowSubType: RowSubTypeID FK_RowTypeID Table
I have table structure as follows id productid ip hittime --------------------------------------------------------------------------- 1 5 1.1.1.1
I have a table structure as follows and I want to select all active
I have the following table structure as follows: (please ignore the blackbox. was formatting
I have a table structure as follows. Now I need to sort these nested
I've got the following problem. My table ( geo_table ) structure is as follows:
I have database table whose structure is as follows: id int name varchar(20) dob

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.