I have chat_notif table like this :
| id | order_id | chat_id | sent_to |
|---|---|---|---|
| 1 | order-001 | 1 | 2 |
| 2 | order-002 | 2 | 1 |
| 3 | order-001 | 3 | 2 |
| 4 | order-001 | 4 | 2 |
| 5 | order-002 | 5 | 1 |
| 6 | order-003 | 6 | 2 |
| 7 | order-003 | 7 | 2 |
| 8 | order-003 | 8 | 2 |
| 9 | order-001 | 9 | 2 |
| 10 | order-002 | 10 | 1 |
What i need is to get count but with group by order_id where sent_to is 2:
SELECT *, count(id) as count FROM chat_notif WHERE sent_to=2 GROUP BY order_id
| id | order_id | chat_id | sent_to | count |
|---|---|---|---|---|
| 1 | order-001 | 1 | 2 | 4 |
| 6 | order-003 | 6 | 2 | 3 |
From the table above, the count is right. But what i need is to get the last id of each group so it should be like this :
| id | order_id | chat_id | sent_to | count |
|---|---|---|---|---|
| 9 | order-001 | 9 | 2 | 4 |
| 8 | order-003 | 8 | 2 | 3 |
How do i do it then?