СC
Size: a a a
СC
A
БГ
SELECT m.id, u.name, CASE WHEN u.id = m.to_id THEN "TO" ELSE "FROM" END AS "direction", m.text
FROM messages m
join users u on
u.id = m.to_id or u.id = m.from_id
where m.to_id = ? or m.from_id = ?
order by id desc
БГ
A
БГ
A
БГ
БГ
БГ
A
A
(select max(m.id) as id, from_id, to_id from messages group by from_id,to_id)
A
A
SELECT m.id, u.name, CASE WHEN u.id = m.to_id THEN "TO" ELSE "FROM" END AS "direction", m.text
FROM messages m
join users u on u.id = m.to_id or u.id = m.from_id
join (select max(id) as id, from_id, to_id from messages group by from_id, to_id) mm on mm.id = m.id
where m.to_id = ? or m.from_id = ?
БГ
SELECT m.id, u.name, CASE WHEN u.id = m.to_id THEN "TO" ELSE "FROM" END AS "direction", m.text
FROM messages m
join users u on u.id = m.to_id or u.id = m.from_id
join (select max(id) as id, from_id, to_id from messages group by from_id, to_id) mm on mm.id = m.id
where m.to_id = ? or m.from_id = ?
e
e
e
БГ
SELECT m.id, u.name, CASE WHEN u.id = m.to_id THEN "TO" ELSE "FROM" END AS "direction", m.text
FROM messages m
join users u on u.id = m.to_id or u.id = m.from_id
join (select max(id) as id, from_id, to_id from messages group by from_id, to_id) mm on mm.id = m.id
where m.to_id = ? or m.from_id = ?
SELECT CASE WHEN m.to_id = ? THEN m.from_id ELSE m.to_id END AS "interlocutor", u.name, m.text
FROM messages m
JOIN users u ON
u.id = m.to_id OR u.id = m.from_id
WHERE m.to_id = ? OR m.from_id = ?
GROUP BY interlocutor
ORDER BY m.id DESC