Jump to content

[SOLVED] Selecting the latest Row from a table in a Join Query


cooldude832

Recommended Posts

I have 3 tables being used in this query

 

Users (UserID is primary key all you need to know here)

Urls (UrlID, Url, UserID (links to Usres)

Urls_checks (BatchID, CheckID(PK) UrlID (links to urls) and a ton of cURL response fields)

 

My query right now looks like


Select urls.UrlID as UrlID , urls.Url as Url , urls.Active as Active , COUNT(urls_checks.UrlID) as Checks_Count ,
MAX(urls_checks.Date) as Last_Check , 
urls_checks.http_code as Last_Code ,
urls_checks.total_time as Last_Time 
from `urls` 
LEFT JOIN urls_checks ON (urls_checks.UrlID = urls.UrlID)
WHERE urls.UserID = '1' GROUP BY urls.UrlID

 

and produces a table of data

my problem is if u see the last part of the select

urls_checks.http_code as Last_Code , urls_check.stotal_time as Last_Time

I want this to select the Latest row for that UrlID How can I do this  (I tried adding a where MAX(urls_checks.Date)  but of course that will fail if another check occurs from a different user and return that response code or will it cause of my join???

 

Link to comment
Share on other sites

try

SELECT urls.UrlID, urls.Url, urls.Active,
(SELECT COUNT(*) FROM urls_checks WHERE UrlID = urls.UrlID) as Check_count,
X.Last_Check , urls_checks.http_code as Last_Code , urls_checks.total_time as Last_Time
FROM urls
JOIN urls_checks ON (urls_checks.UrlID = urls.UrlID)
JOIN (SELECT UrlID, MAX(Date) as Last_Check FROM urls_checks GROUP BY UrlID) as X
ON urls_checks.UrlID = X.UrlID AND urls_checks.Date = X.Last_Check
WHERE urls.UserID = '1'

Link to comment
Share on other sites

That's right. Think of X as a separate table containing the urlID and maxdate so we can join to it to get the data from the matching urls_checks record.

 

If you just do a GROUP BY you get the details from the first record in the group.

 

Because its not doing a GROUP BY we do a sepatate subquery to get the COUNT

Link to comment
Share on other sites

try

SELECT urls.UrlID, urls.Url, urls.Active,
(SELECT COUNT(*) FROM urls_checks WHERE UrlID = urls.UrlID) as Check_count,
Y.Last_Check , Y.http_code as Last_Code , Y.total_time as Last_Time
FROM urls
LEFT JOIN (SELECT X.Last_Check, urls_checks.UrlID, urls_checks.http_code, urls_checks.total_time
FROM urls_checks
JOIN (SELECT UrlID, MAX(Date) as Last_Check FROM urls_checks GROUP BY UrlID) as X
ON urls_checks.UrlID = X.UrlID AND urls_checks.Date = X.Last_Check) as Y
ON urls.UrlID = Y.UrlID
WHERE urls.UserID = '1'

 

@lampstax,

IT Professional for last 40 years but only the last 20 or so with databases and SQL

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.