If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select record with MAX(field) via join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-07, 11:13
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Select record with MAX(field) via join

Again, I'm sure there's a proper name for this... errr... technique? But I have no idea what it is so you're stuck with my wobbly description. Sorry

Lets' say I've got two tables: tblFiles, and tblFileTitles

tblFiles:
id
file_title_id
url
version_number


tblFileTitles:
id
title


You get the gist: it's a file versioning system. But how do I get the latest version of each file (or rather, file TITLE)?

I tried this:

SELECT
f.id AS fileID,
MAX(f.version_number) AS latestVersion,
f.url
ft.id AS titleID,
ft.title
FROM tblFiles f
INNER JOIN tblFileTitles ft ON f.file_title_id = ft.id
GROUP BY ft.id

Which would be great except that the file ID (and URL) that it's pulling out isn't the one that corresponds to the latest version. In fact, it seems to be pulling out the first record from tblFiles that has the corresponding title_id.

Am I making sense? Can someone explain how to get the "latest" file in there?
Reply With Quote
  #2 (permalink)  
Old 06-13-07, 11:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
since you asked "via join" --
Code:
SELECT f.id
     , f.file_title_id
     , f.url
     , f.version_number
  FROM tblFiles f
INNER 
  JOIN (
       SELECT file_title_id
            , max(version_number) as max_version
         FROM tblFiles 
       GROUP
           BY file_title_id
       ) as m
    ON m.file_title_id = f.file_title_id
   AND m.max_version   = f.version_number
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-13-07, 12:05
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Thanks - that's exactly the job I've got to ask, though - it sounds like there's a better way?
Reply With Quote
  #4 (permalink)  
Old 06-13-07, 12:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
better? not necessarily
Code:
SELECT f.id
     , f.file_title_id
     , f.url
     , f.version_number
  FROM tblFiles f
 WHERE f.version_number =
       ( SELECT max(version_number)
           FROM tblFiles 
          WHERE file_title_id = f.file_title_id )
to me, it's a bit easier to understand what this query is doing, with its correlated subquery, as compared to the one with the join

next, you're going to ask which is more efficient, aren't you

the answer is: you need to test them yourself
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-13-07, 15:15
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Quote:
Originally Posted by r937
correlated subquery
That's it! I knew there was a trick you could do with a subquery, but could never get my head round joining it to the parent. I should really get myself some sort of theoretical grounding in this, shouldn't I? Thank you, though
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On