+----+-------------+------------+--------+--------------+------------+
| id | message | date | status | from_user_id | to_user_id |
+----+-------------+------------+--------+--------------+------------+
| 1 | sfgsfdgsfdg | 1693310921 | 0 | 15 | 20 |
| 2 | aboba | 1693317654 | 0 | 20 | 15 |
| 3 | 123 | 1693318102 | 0 | 19 | 15 |
| 4 | 321 | 1693318124 | 0 | 15 | 19 |
+----+-------------+------------+--------+--------------+------------+
SELECT
messages.*,
users.login,
image.img
FROM
messages
LEFT JOIN
users ON messages.to_user_id = users.id
LEFT JOIN
image ON messages.to_user_id = image.obj_id AND image.obj_type = 'user'
WHERE
messages.date = (SELECT MAX(date) FROM messages WHERE from_user_id = :id AND to_user_id = :id)
+----+---------+------------+--------+--------------+------------+
| id | message | date | status | from_user_id | to_user_id |
+----+---------+------------+--------+--------------+------------+
| 1 | ... | 1693310921 | 0 | 15 | 20 |
| 2 | ... | 1693317654 | 0 | 20 | 15 |
| 3 | ... | 1693318102 | 0 | 19 | 15 |
| 4 | ... | 1693318124 | 0 | 15 | 19 |
+----+---------+------------+--------+--------------+------------+
+----+---------+
| id | login |
+----+---------+
| 15 | user1 |
| 19 | user2 |
| 20 | user3 |
+----+---------+
SELECT
m1.*,
u1.login AS from_user_login,
u2.login AS to_user_login
FROM
messages m1
JOIN
(
SELECT
LEAST(from_user_id, to_user_id) AS user1_id,
GREATEST(from_user_id, to_user_id) AS user2_id,
MAX(date) AS max_date
FROM
messages
GROUP BY
LEAST(from_user_id, to_user_id),
GREATEST(from_user_id, to_user_id)
) m2 ON LEAST(m1.from_user_id, m1.to_user_id) = m2.user1_id
AND GREATEST(m1.from_user_id, m1.to_user_id) = m2.user2_id
AND m1.date = m2.max_date
JOIN
users u1 ON m1.from_user_id = u1.id
JOIN
users u2 ON m1.to_user_id = u2.id;
SELECT m.*, u.login, i.img
FROM messages m
LEFT JOIN users u
ON m.to_user_id = u.id
LEFT JOIN image i
ON m.to_user_id = i.obj_id
WHERE m.date > :lastdate # надо выбирать все что позже уже полученных сообщений
AND image.obj_type = 'user'
AND m.from_user_id = :fid # айди "от юзера"
AND m.to_user_id = :tid #айди "к юзеру"
ORDER BY m.date # по возрастанию все старше последнего полученного
public function dialogueList($id)
{
try
{
$query = "
SELECT
m.message,
m.date,
u.login AS interlocutor_name,
i.img AS interlocutor_img
FROM messages m
JOIN (
SELECT MAX(date) AS max_date, dialog_id
FROM messages
WHERE from_user_id = :id1 OR to_user_id = :id2
GROUP BY dialog_id
) t ON m.dialog_id = t.dialog_id AND m.date = t.max_date
JOIN users u ON
(CASE
WHEN m.from_user_id = :id3 THEN m.to_user_id = u.id
WHEN m.to_user_id = :id4 THEN m.from_user_id = u.id
END)
LEFT JOIN
image i ON u.id = i.obj_id AND i.obj_type = 'user'
WHERE m.from_user_id = :id5 OR m.to_user_id = :id6
";
$stmt = $this->db->prepare($query);
$stmt->bindValue(":id1", $id, PDO::PARAM_INT);
$stmt->bindValue(":id2", $id, PDO::PARAM_INT);
$stmt->bindValue(":id3", $id, PDO::PARAM_INT);
$stmt->bindValue(":id4", $id, PDO::PARAM_INT);
$stmt->bindValue(":id5", $id, PDO::PARAM_INT);
$stmt->bindValue(":id6", $id, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll();
return $data;
}
catch(PDOException $e)
{
echo $e;
}
}