List:General Discussion« Previous MessageNext Message »
From:Peter Brawley Date:April 18 2008 3:07pm
Subject:Re: A SQL Query Question
View as plain text  
>userId long
>picture MeduimBlob
>datePosted DateTime
>A userId can have many pictures posted. I want to write a 
>query that returns a distinct userId along with the most 
>recent picture posted. Can someone suggest an elegant and 
>fast query to accomplish this?

Latest pic for user N:

SELECT userID,MAX(dateposted)
FROM tbl
WHERE userID=N;

Latest pics per user:

SELECT t1.userID,t1.dateposted
FROM tbl t1
LEFT JOIN tbl t2 ON t1.userID=t2.userID AND t1.dateposted<t2.dateposted
WHERE t2.userID IS NULL;

PB
Thread
A SQL Query Questionsamk18 Apr
  • Re: A SQL Query QuestionPeter Brawley18 Apr
Re: A SQL Query Questionsamk18 Apr