So, i have this table structure
+----+---------+-----------+---------+------+---------------------+
| id | to_user | from_user | message | read | sent |
+----+---------+-----------+---------+------+---------------------+
| 1 | 2 | 1 | test | 0 | 2012-01-11 13:20:14 |
+----+---------+-----------+---------+------+---------------------+
And as you can see i use this for very simple chat, and now what i wanna do is get the list of each conversations. So i need to find a way of selecting one last message from conversation which each user. so i need something like
select *
from `messages`
where `to_user` = 2 --(this is my user id)
and `to_user` = `from_user`
LIMIT 1
Now i know that LIMIT would always return 1 record and other logic is probably not so good, anyways if anyone get’s what i want here, could help me a little 🙂
An
IN()subquery selects theidof each row having the most recentsentbetween two users. The outer query selects the remaining columns for eachidreturned from the subquery.Update fixed the HAVING clause…