Plz help me to get Mutual Friends sql

my Sql

Following Query = "SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `following_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` = {$user_id} AND `following_id` <> {$user_id} AND `active` = '1') AND `active` = '1' ";

Follower Query = " SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `follower_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` <> {$user_id} AND `following_id` = {$user_id} AND `active` = '1') AND `active` = '1'";

But i can not get Mutual Friends
sql.jpg
Sajal KoleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What defines a mutual friend - follower / following or both?
Pawan KumarDatabase ExpertCommented:
Can you please provide input rows and the expected output? With screen shot things are not clear.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Try adding a DESCRIBE of table(s) involved + likely someone can assist.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Snarf0001Commented:
Just a guess since you haven't provided much detail, but I'm assuming in this case you're going to have TWO user_id values, and you're trying to see all the people that BOTH of them are following?

Mutual Followers (user_id1, user_id2)
select u.user_id
from T_Followers f1
join T_Followers f2 on f1.following_id = f2.following_id
join T_Users u on f1.following_id = u.user_id
where u.Active = 1 and f1.active = 1 and f1.follower_id = {$user_id1} and f2.active = 1 and f2.follower_id = {$user_id2}

Open in new window





FYI, you can simplify your initial queries as well if you start joining instead of using "in" subqueries.

Following (user_id)
select u.user_id
join T_Followers f
join T_Users u on f.following_id = u.user_id
where f.follower_id = ${user_id} and f.following_id <> ${user_id} and u.active = 1 and f.active = 1

Open in new window


Follower (user_id)
select u.user_id
join T_Followers f
join T_Users u on f.following_id = u.user_id
where f.follower_id <> ${user_id} and f.following_id = ${user_id} and u.active = 1 and f.active = 1

Open in new window

Sajal KoleyAuthor Commented:
Sir i have a relation table and as u so on this pic. and can i get mutual friend or mutual followers ?

following code
SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `following_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` = {$user_id} AND `following_id` <> {$user_id} AND `active` = '1') AND `active` = '1' 

Open in new window


follower code
 SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `follower_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` <> {$user_id} AND `following_id` = {$user_id} AND `active` = '1') AND `active` = '1'

Open in new window



can i get mutual friend with a user_id and a relation table ?
sql.jpg
Pawan KumarDatabase ExpertCommented:
Please try this -

SELECT *
FROM 
(
	" SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` 
	IN (SELECT `follower_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` <> {$user_id} 
	AND `following_id` = {$user_id} AND `active` = '1') AND `active` = '1'"
)k
INNER JOIN 
(
 "SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` 
 IN (SELECT `following_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` = {$user_id} 
 AND `following_id` <> {$user_id} AND `active` = '1') AND `active` = '1' "
)r ON r.`user_id`  = k.`user_id`

Open in new window

Snarf0001Commented:
Okay, I'm taking a second guess at what you want then.
Now guessing that by "mutual", you actually mean a list of everyone that I'm following, who is ALSO following me?

In which case this should do it:
select u.user_id
from T_Followers f1
join T_Followers f2 on f1.following_id = f2.follower_id and f1.follower_id = f2.following_id
join T_Users u on f1.following_id = u.user_id
where u.Active = 1 and f1.active = 1 and f1.follower_id = {$user_id1} 

Open in new window


If that's not it, then please clarify exactly what you mean by "mutual".

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase ExpertCommented:
question abandoned. Provided multiple solutions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.