There are two tables: dialog(id,firstname) and message(id, dialog_id, text, time) . You must select all dialog entries, with each dialog entry selecting the last message entry associated with this dialog
dialog.id | dialog.firstname | message.text | message.time
Please your help, comrades.