I have two tables : orders and clients. Clients have one or more Orders (JOIN condition : clients.id = orders.client_id
).
I need to retrieve every client with their most "recent" order, based on a date column (named created) in orders table, in one request if possible.
Everything I tried retrieve each client with the first order found, not the last (and I can't rely on the primary key for that).
How should my request look like?
I could try a subquery with a MAX(created)
but two problems:
- There could be more than one order with the same date. I'd need to order them by ID but it's impossible inside a subquery?
- I need to retrieve more than just the date column.