Jump to content

how do i always capture the newest record?


bcamp1973

Recommended Posts

for versioning i have table that always updated via INSERTs instead of UPDATEs so i have a history of previous changes and i can easily roll back. what's the most efficient way of always querying for the newest record?  i could go by the 'updated' filed which is a timestamp, or the 'id' field which auto-increments. is there a built in MySQL function for this?

Link to comment
Share on other sites

well, here are my tables...

 

NOTES_TYPES

-----------------

id

type

 

NOTES

-----------------

id

type_id

project_id

note

 

...and my query...

 

SELECT type, type_id, notes.id, note FROM notes LEFT JOIN notes_types ON notes.type_id=notes_types.id WHERE project_id=xxx GROUP BY type_id

 

So, say a note can be any of the following types

- comment (id #1)

- complaint (id #2)

- request (id #3)

 

and in the NOTES table has multiple entries of each.  I want my query to capture the most recent entry (highest notes.id #) of each of the 3 types, but not the previous entries...how does my query have to change to accomplish this?

Link to comment
Share on other sites

I don't think that's what you mean.

 

Why not? Id is an autoinc field so the highest one will always be the most recent.

 

SELECT type, type_id, notes.id, note 
FROM notes 
JOIN (
    SELECT MAX(id) AS max_note, type_id
    FROM notes
    WHERE project_id=xxx
    GROUP BY type_id
) AS sub ON note.id = sub.max_note
LEFT JOIN notes_types ON notes.type_id=notes_types.id 
WHERE project_id=xxx 

Link to comment
Share on other sites

No problem. Lets see what Fenway was talking about first though. Maybe he's seeing something that I'm missing.

Nothing's "missing" per se... I just don't like to use UIDs for anything other than relationships.  If you want to most recent one, use a datetime/timestamp column for sorting.  There doesn't even have to be a uid at all, and the value of this field can be anything, even if it is auto-incremented.

Link to comment
Share on other sites

fenway, thanks for following up.  i guess i thought that since the 'id' was an INT and indexed it would be much quicker to query on than the DATETIME field (even if it's indexed) but I guess not (I'm very much a newbie)? The application i'm building isn't going to get high traffic so honestly it's not a big issue, however, i'm trying to learn as I go so I reduce mistakes on future apps :) Can you demonstrate how you'd create that query? Would it simply be a tweak to artacus' query above?

Link to comment
Share on other sites

I'm siding with bcamp on this one. I'm not sure if mysql handles handles dates as quickly as it does INT. But if one was faster, I'm sure it would be INT. Getting the max value from an indexed column though, will be way faster than on a non-indexed one. And I don't see the point of adding another index to do something we can already do with the PK.

Link to comment
Share on other sites

Well, yes, you'd have to index the datetime field appropriately... I just like the queries doing what it's supposed to be doing, and not trying to tweak it for, as you say, a non-high traffic app.  It would be a minor tweak, simply getting find the record with MAX( datetimefield ) and then using it's UID in the join.

 

For example, what if the record was updated and the timestamp changed so that max(uid) <> max(timestamp)?  It's definitely possible, so why break it to begin with?

Link to comment
Share on other sites

For example, what if the record was updated and the timestamp changed so that max(uid) <> max(timestamp)?

That would be correct, however, bcamp prefaced the question with:

i have table that always updated via INSERTs instead of UPDATEs

 

Never assume that "always" is going to stay "always" forever...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.