A
A
Alex2019-07-11 17:55:35
PostgreSQL
Alex, 2019-07-11 17:55:35

Sorting users by latest entry in posts table, how to query?

Good afternoon.
There are tables
users - contains users.
messages - contains a bunch of messages, where there is createdAt
We need to get users and sort them in chronological order by messages.
How can this be done without requests? Maybe there are some specialized aggregate functions

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-07-11
@sensus

select u.* from users as u
inner join messages as m
  on u.id = m.user_id
group by u.id
order by max(m.createdAt);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question