Answer the question
In order to leave comments, you need to log in
How to group data "by pairs" in MySQL?
There is a table in which there are discussions. The structure is like this:
from_id - от кого
to_id - к кому
p_id - предмет обсуждения
message - сообщения
from_id | to_id | p_id | message
1 | 2 | 5 | привет
2 | 1 | 5 | и тебе привет
Answer the question
In order to leave comments, you need to log in
SELECT * FROM msg WHERE from_id=2 OR to_id=2 AND p_id=5;
Something like this
For example like this:
SELECT DISTINCT
p_id,
CASE WHEN temp.var = 0 THEN from_id ELSE to_id END user_id
FROM table
CROSS JOIN (SELECT 0 var FROM DUAL UNION ALL SELECT 1 var FROM DUAL) temp
p_id user_id
5 1
5 2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question