select t2.* from table1 t1,table1 t2
where
t1.from_user_id=t2.to_user_id and
t2.from_user_id=t1.to_user_id and
t1.from_user_id=5
CREATE TABLE `frends` (
`from_user_id` INT(11) NULL DEFAULT NULL,
`to_user_id` INT(11) NULL DEFAULT NULL
);
INSERT INTO `frends` (`from_user_id`, `to_user_id`) VALUES
(1, 3),
(3, 1),
(1, 2),
(1, 4),
(4, 1),
(3, 4),
(5, 1),
(5, 4),
(4, 5),
(1, 5);
select frends.from_user_id 'Я', group_concat(frendsPlus.from_user_id) 'Мы друзья'
from frends
left join frends frendsPlus on
(
frends.from_user_id = frendsPlus.to_user_id and
frendsPlus.from_user_id = frends.to_user_id
)
group by frends.from_user_id