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

The Archive Base Latest Questions

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

I know this is a common question, because I’ve asked it before, and have

  • 0

I know this is a common question, because I’ve asked it before, and have looked at several more, but still, despite basing my query on this, I cannot get it to work. see: Mysql join based on max(timestamp) and mysql LEFT join for right table max value

Basically I have a tables of different items (‘chairs’, ‘tables’) and each type has record with their own id for specific chairs or tables. Then I have a location table which keeps all the locations that have ever been, including the current one. The current location is noted by the MAX timestamp.

My query is meant to get all the items, and join to the current location. I have followed what others have said works, but it isn’t working for me. Any help is appreciated.

EDIT: I didn’t say how it fails: It gets records and makes the join, and it returns the max timestamp, but not the record info that corresponds with the MAX(timestamp) record. So it gives the right timestamp, but the floor and etc are NOT from the record with the MAX timestamp.

Thanks!

The query:

$q = "SELECT
        'chairs' AS work_type,
        chairs.id AS work_id,
        chairs.title,
        chairs.dimensions,
        CurrentLocations.floor,
        CurrentLocations.bin,
        CurrentLocations.bay,
        CurrentLocations.other
       FROM chairs
             LEFT JOIN ( 
                        SELECT 
                              locations.id AS loc_id,
                              locations.work_type AS clwt,
                              locations.work_id AS clwi,
                              locations.floor,
                              locations.bin,
                              locations.bay,
                              locations.other,
                              MAX(locations.timestamp) AS latestLocation
                            FROM
                               locations
                            GROUP BY
                               locations.work_type, locations.work_id
                            ) CurrentLocations
                ON CurrentLocations.clwi = chairs.id AND CurrentLocations.clwt = 'chairs'
        WHERE cur_loc LIKE '%19th Street%'
        ORDER BY 
            FIELD(floor, 'Basement', '3rd Floor', '4th Floor', '5th Floor'), 
            bin, 
            bay,
            other";
  • 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-17T13:26:09+00:00Added an answer on June 17, 2026 at 1:26 pm

    Try this:

        SELECT
            'chairs' AS work_type,        -- do you really need this in the result?
            c.id AS work_id,
            c.title,
            c.dimensions,
            l.floor,
            l.bin,
            l.bay,
            l.other
        FROM 
            chairs AS c
          LEFT JOIN
            ( SELECT work_id,
                     MAX(timestamp) AS latestLocation
              FROM   locations
              WHERE  work_type = 'chairs'
              GROUP BY work_id
            ) AS cl                                  -- CurrentLocations
              ON  cl.work_id = c.id
          LEFT JOIN
            locations AS l 
              ON  l.work_type = 'chairs'
              AND l.work_id = cl.work_id
              AND l.timestamp = cl.latestLocation 
        WHERE 
            c.cur_loc LIKE '%19th Street%'
        ORDER BY 
            FIELD(l.floor, 'Basement', '3rd Floor', '4th Floor', '5th Floor'), 
            l.bin, 
            l.bay,
            l.other ;
    

    Description:

    You want:

    Basically I have a tables of different items (chairs, tables) and each type has record with their own id for specific chairs or tables. Then I have a locations table which keeps all the locations that have ever been, including the current one. The current location is noted by the maximum timestamp.

    My query is meant to get all the items (chairs), and join to the current location.

    So, for every item (that is a chair) you want only the latest location. For every work_id in table location, you want the MAX(timestamp). This can be easily found with a GROUP BY, as you already found out:

            ( SELECT work_id,
                     MAX(timestamp) AS latestLocation
              FROM   locations
              GROUP BY work_type
                     , work_id
            ) AS cl
    

    But you also want to use other columns from that table and if you simply add them in the select list, it shows wrong results. (Note that in most DBMS, in order to use columns that are not in the GROUP BY list, you’d have to apply an aggregation function, like you applied MAX() to timestamp. MySQL allows this behaviour which if properly used, can have some benefits. But it hasn’t implemented it 100% failproof and thus applying it in some cases, like this one, can give wrong results. Postgres has a much better implementation of this feature that it won’t allow erroneous results.)

    So, what you have to do is use the above GROUP BY query as a derived table (you name it cl or CurrentLocation, whatever you want) and join it to the locations table, using all columns from the GROUP BY list and the aggregated one (timestamp):

          JOIN
            locations AS l 
              ON  l.work_type = cl.work_type
              AND l.work_id = cl.work_id
              AND l.timestamp = cl.latestLocation 
    

    After that, you can join them to the chairs table as usual. Since, you had a chairs LEFT JOIN locations in the original query, I kept these in the answer, too.

    Another minor modification was that since you had CurrentLocations.work_id = 'chairs', you wanted only those rows from the locations table. So, it’s rather redundant to do first the grouping and then reject all rows that don’t match that condition. It’s probably more efficient to first apply the condition and then group by. The final grouping query and the join conditions are modified accordingly.

    Also note that an index on (work_type, work_id, timestamp) will improve efficiency of the query (the cl subquery will be processed using only that index and the JOIN locations will use the index, too, to locate the rows needed from the locations table.)

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

Sidebar

Related Questions

I know this might be a common question but I have tried to search
Sorry I know similar question have been asked many times before but like most
I know this question has been asked a couple of times before. I m
I know this is an old question, but I have spend any hours on
I think this is a common and typical question from novices but I still
I know this a common question but everything I found seems to remove white
I know this question has been asked many times and we have gotten this
I know the question sound somewhat stupid, but i have this scenario. Context I
This may be a more math focused question, but wanted to ask here because
I know this is a common question but I just can't seem to get

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.