I've just started learning SQL, so I apologies if this question is a bit of a dummy question.
I have two tables:
direct_messages
users
A direct_messages
table looks like this:
receiverId
and senderId
are foreign keys to my users
table.
What I'm trying to achieve is a typical inbox message query. Meaning get one message of each message sent that has the same receiverId
as the logged in user and also get the latest one.
I've tried to get this working almost the whole day and I've come this "far":
SELECT *
FROM "direct_messages" tm
LEFT JOIN "users" tu ON "tm.senderId" = "tu.id"
WHERE "tm.createdAt"
ORDER BY "created_at" DESC
LIMIT 1;
But I'm just getting ERROR: column "tm.senderId" does not exist
.
All the help I can get is super appreciated. Thank you!
EDIT: As requested, I added some example data: https://gist.github.com/Martinnord/f07bc62389f2ecd0df8e6716dd797a15
What I want is to get a list of messages that has a particular receiverId
, but only one from each unique senderId
and get the latest one. Like a typical message inbox.