Let's say I have three tables:
users with identifiers of namesemails containing strings that have their email addressmessages containing strings containing the dates and names of their letters
the second and third tables can be mapped to the first using a user ID.
I need a request that each user will return only once, his email address and the date of their last message.
If I use this:
SELECT name,email,title,date FROM users JOIN emails ON users.id = emails.user_id JOIN messages ON messages.user_id = emails.user_id group by name order by date desc
I do not receive the last message because the order occurred after joining and grouping. I receive one email from my users, and emails are sorted by date.
Can this be done in one connection? What am I missing?
Pastebin for a dummy database you can use: http://pastebin.com/1x273aEe - the actual database is not quite the same, but the same problem.
source share