Your query is not valid, because row_num
is not defined for the where
clause. In MariaDB, you would need a subquery or CTE. Let me just assume you oversimplified the query and you want the ticket_response
with the most recent time
.
I would suggest a correlated subquery:
SELECT *
FROM tickets t JOIN
ticket_responses tr
ON tr.ticketId = t.id
WHERE tr.time = (SELECT MIN(tr2.time)
FROM ticket_responses tr2
WHERE tr2.ticketId = tr.ticketId
);
Note: This is not exactly the same. It is actually equivalent to rank()
-- but if time
is unique, it returns the same results.
If time
may not be unique, then you need some way to separate the duplicates on time. If I assume that there is a unique id in ticket_responses
, then:
SELECT *
FROM tickets t JOIN
ticket_responses tr
ON tr.ticketId = t.id
WHERE tr.ticket_responses_id = (SELECT tr2.ticket_responses_id
FROM ticket_responses tr2
WHERE tr2.ticketId = tr.ticketId
ORDER BY tr2.time DESC, tr2.ticket_responses_id DESC
LIMIT 1
);