I
I
IceJOKER2014-07-26 15:08:50
MySQL
IceJOKER, 2014-07-26 15:08:50

How to display possible friends (sql query)?

You need to display all users with common friends.
for the current user (let it be with id 1), friends are selected like this:
table structure:
to_id, from_id
1 2
5 1
3 2
3 5
//here the user with id-1 of possible friends 1 is the user with id 3

select if(to_id = 1 , from_id, to_id) friend_id from friends_table f1 where f1.to_id = 1 or f1.from_id = 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vdem, 2014-07-26
@vdem

Not sure, maybe like this:

SELECT *
FROM friends f
    INNER JOIN friends f1 ON f1.from_id = f.to_id
    INNER JOIN friends f2 ON f2.to_id = f.from_id
WHERE f.from_id = 1
    OR f.to_id = 1

Then from the result (both columns and rows) remove duplicate id.

A
Alexander Taratin, 2014-07-26
@Taraflex

SELECT to_id FROM friends_table WHERE to_id IN (SELECT from_id FROM friends_table WHERE to_id = 1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question