БГ
Size: a a a
БГ
БГ
SELECT CASE WHEN m.to_id = 1 THEN m.from_id ELSE m.to_id END AS "interlocutor", u.name, m.id, m.text
FROM messages m
JOIN users u ON
u.id = m.to_id OR u.id = m.from_id
WHERE m.to_id = 1 OR m.from_id = 1
GROUP BY interlocutor
HAVING m.id = max(m.id)
ORDER BY m.id DESC
БГ
БГ
AT
AT
AT
БГ
БГ
SELECT
CASE WHEN m.to_id = ? THEN sender.id ELSE receiver.id END interlocutor,
CASE WHEN m.to_id = ? THEN sender.name ELSE receiver.name END username,
sender.name msg_sender,
m.text msg_text
FROM messages m
JOIN users sender ON
sender.id = m.from_id
JOIN users receiver ON
receiver.id = m.to_id
WHERE m.to_id = ? OR m.from_id = ?
GROUP BY interlocutor
HAVING m.id = max(m.id)
ORDER BY m.id DESC
БГ
AT
БГ
👑️
SELECT
CASE WHEN m.to_id = ? THEN sender.id ELSE receiver.id END interlocutor,
CASE WHEN m.to_id = ? THEN sender.name ELSE receiver.name END username,
sender.name msg_sender,
m.text msg_text
FROM messages m
JOIN users sender ON
sender.id = m.from_id
JOIN users receiver ON
receiver.id = m.to_id
WHERE m.to_id = ? OR m.from_id = ?
GROUP BY interlocutor
HAVING m.id = max(m.id)
ORDER BY m.id DESC
AT
БГ
БГ
K
БГ
K
БГ