MYSQL query, distinct on one column

I have a table that looks like this


id, from_user_id, to_user_id, date

1, 1, 2, 2019-09-19 17:19:18
2, 1, 2, 2019-09-19 17:19:36
3, 6, 2, 2019-09-19 17:28:33

I want every row where to_user_id = 2
but I want from_user_id to be distinct
and it needs to be order by date desc


This is what im hoping for:

id, from_user_id, to_user_id, date

2, 1, 2, 2019-09-19 17:19:36
3, 6, 2, 2019-09-19 17:28:33


Is there a simple/clean solution without joins?
I’ve tried group by and subqueries, but cant get it right.

no problemo

SELECT from_user_id
     , MAX(date) AS latest
  FROM daTable
 WHERE to_user_id = 2  
GROUP
    BY from_user_id
1 Like

Thanks man. It’s appreciated

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.