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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:13:19+00:00 2026-06-01T21:13:19+00:00

This one’s been tricky to quantify, so I may not have this question worded

  • 0

This one’s been tricky to quantify, so I may not have this question worded properly first time around.

I have a table following a format similar to this:

| id | other_id | timestamp  |
|  1 |        1 | 2012-01-01 |
|  2 |        1 | 2012-01-02 |
|  3 |        2 | 2012-01-02 |

What I am attempting to do is, given the record with ‘id’ 2, and similar records, for which the ‘id’ column value is known and is unique and the ‘other_id’ is known corresponding with it, how do I find, for each, the ‘id’ of the record having the same ‘other_id’ but the first lower ‘id’ than the one I already know.

E.g.

$arrKnownIds = array (
 0 => array('id'=>2,'other_id'=>1),
 1 => array('id'=>3,'other_id'=>2)
);

With this info, I’d like to run a query such that this results:

while($row = mysql_fetch_assoc($result)) {
 $arrPreviousIds[$row['other_id']] = $row['id'];
 // having in this case values of:
 // $row['other_id'] = 2;
 // $row['id'] = 1;
}

I can’t quite work out if I need to tackle this using UNION, multiple php query statements or if there’s another way.

Any thoughts on how to tackle this one are greatly appreciated.

Thanks 🙂

Edit – The original query takes the following form:

SELECT DISTINCT(`other_id`), MAX(`id`), MAX(`timestamp`)
FROM `event`
GROUP BY `other_id`
ORDER BY `id` DESC, `other_id` ASC
LIMIT 0, 10

// This is intended to get the last 10 unique events and find when they occurred.

// From this, I then try to find when they previously occurred.

  • 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-01T21:13:20+00:00Added an answer on June 1, 2026 at 9:13 pm

    How about this?

    SELECT t1.id, (SELECT id
                   FROM tbl t2
                   WHERE t2.other_id = t1.other_id
                   AND t2.id < t1.id
                   ORDER BY t2.id DESC
                   LIMIT 1)
    FROM tbl t1
    WHERE t1.id IN (1,2,3)
    

    There are more efficient ways of doing this if you will be dealing with large result sets. Can you explain exactly how you will be using this query?

    UPDATE – based on addition of existing query to question here is an updated query to combine the two –

    SELECT tmp.*, (SELECT `timestamp`
                   FROM `event`
                   WHERE `event`.`other_id` = `tmp`.`other_id`
                   AND `event`.`id` < `tmp`.`id`
                   ORDER BY `event`.`id` DESC
                   LIMIT 1) AS `prev_timestamp`
    FROM (
        SELECT `other_id`, MAX(`id`) AS `id`, MAX(`timestamp`) AS `timestamp`
        FROM `event`
        GROUP BY `other_id`
        ORDER BY `id` DESC, `other_id` ASC
        LIMIT 0, 10
    ) tmp
    

    I have not tried this but it should give the desired result.

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

Sidebar

Related Questions

this one is not homework, it just the fact that i've been out of
This one has been driving me nuts. I have a div that has it's
This one may be a bit strange. I have a solution in Visual Studio
This one may seem like a simple question, but it's really got me scratching
This one is a bit tricky to explain. I have a usercontrol with some
This one has been stumping me for a while. But I'm no expert. This
This one has proven to be a little tricky for me so far. I
This one is tough to explain inside the title. What I have going on
This one is probably a simple one, but I could not find an example
This one is a little strange to me. I have a pointer to a

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.