I have two tables, tbl_msg and tbl_details.(Like the photo below)
I want to select the last message of each chat, like the image embedded with SQL, such as WhatsApp and Telegram chat lists.(sql code).
please help me. thank’s
Question posted in Telegram API
A comprehensive official documentation can be found here.
A comprehensive official documentation can be found here.
3
Answers
This might be one option:
One method is window functions:
Note: You do not need
tbl_msg
unless there are columns from the table you want in the result set.Also, it is unclear if the "last message" is based on the time column or the id column. This uses the time column, but you can — of course — use the id if that is your definition of "last".
Maybe with a "group by" and an "HAVING c.details_id = MAX(c.details_id)" it’s better no ?
Like that :