In my DB, there are two types of images: challenges, and answers. They both have lat/lng location columns. In this query, I wish to select the Challenge or Answer that the user created most recently via an INNER JOIN.
(The business logic behind this: we basically want to obtain a list of users which includes the user's last known location, which is determined by their most recent Challenge or Answer - whichever is most recent. If a user does not have a last known location, they should not be included in this list.)
I am getting [Err] 1054 - Unknown column 'U.Id' in 'where clause'
:
SELECT
U.Id,
U.TotalPoints,
LastImage.Lat,
LastImage.Lng
FROM User U
INNER JOIN
(
SELECT Lat, Lng FROM
(
(SELECT Lat, Lng, CreatedOn FROM AnswerImage WHERE UserId = U.Id ORDER BY Id DESC LIMIT 1)
UNION ALL
(SELECT Lat, Lng, CreatedOn FROM ChallengeImage WHERE UserId = U.Id ORDER BY Id DESC LIMIT 1)
) LastImages ORDER BY CreatedOn DESC LIMIT 1
) LastImage
WHERE U.Type = 1 AND U.Status = 2
ORDER BY TotalPoints DESC;
I cannot seem to reference the User table (alias U) from within my derived 'LastImages' table (or whatever the proper term for it is).
Can anyone help? I've tried other methods, but none meet all my requirements:
- A row is only returned if the user has at least 1 challenge or 1 answer (hence the UNION)
- The most recent (as determined by ChallengeImage.CreatedOn and AnswerImage.CreatedOn) image is to be used in the join
Thanks!