Reputation: 1411
I'm trying to get mutual friends between 2 people. I've saved the persons friends in a table called "friends" with the following fields:
id | facebook_id | name | uid | timestamp
Hope it make sense, have tried various ways to get the friends, but without luck
Upvotes: 0
Views: 835
Reputation: 331
I dont know if using subqueries would be faster or mySQL already optimizes it. Solution with subqueries:
SELECT f1.id, f1.name
FROM (SELECT id, name FROM friends WHERE uid=1) f1
JOIN (SELECT id, name FROM friends WHERE uid=2) f2
ON f1.id=f2.id;
Upvotes: 2
Reputation: 16362
It would be nice if MySQL has an INTERSECT
operator, but...
You can grab all the friends of one person:
SELECT id, name
FROM friends
WHERE uid = 1
You could then JOIN this list back to the friends table, looking for the same friend for the other user:
SELECT f1.id, f1.name
FROM friends f1
JOIN friends f2 on f1.id = f2.id
WHERE f1.uid = 1
and f2.uid = 2
It's pseudo-code, but it should be close.
Upvotes: 0