To make things simple, I have two tables for a chatbox: Conversation
and Message
Conversation
id | status |
---|---|
1 | open |
2 | open |
Message
idConversation | text | date |
---|---|---|
1 | 'ffff' | (random date) |
1 | 'asdf' | (random date) |
1 | '3123123123' | (random date) |
2 | 'asdfasdff' | (random date) |
2 | 'asdfasdfcas' | (random date) |
2 | 'asdfasdfasdf' | (random date) |
I can select all the Conversation
easily enough by doing:
await Conversation.query().where({
status: 'open',
})
But I am trying to join these together into one query to get 1 message per conversation. This is the query I have for it right now:
await Conversation.query()
.where({
status: 'open',
})
.innerJoin('Message', 'Message.idConversation', 'Conversation.id')
.distinct('Message.idConversation')
.select('Message.idConversation', 'Message.text')
But this is yielding a result as:
[
{
idConversation: 1,
text: 'ffff'
},
{
idConversation: 1,
text: 'asdf'
},
{
idConversation: 1,
text: '3123123123'
},
....
]
I would just like one message per conversation id such as:
{
idConversation: 1,
text: 'ffff'
},
{
idConversation: 2,
text: 'asdfasdff'
},
How could I fix this query?
This is it raw:
SELECT u.id, p.text
FROM Conversation AS u
INNER JOIN Message AS p ON p.id = (
SELECT id
FROM Message AS p2
WHERE p2.idConversation = u.id
LIMIT 1
)