-1

I want to get name, email, id of both users who followed each other and status equal to 'A'.

My friends table screenshot:

follower/friends table screenshot

Note: In the above screenshot follower_id and following_id are both users table ids.

philipxy
  • 14,867
  • 6
  • 39
  • 83
M-Khawar
  • 855
  • 5
  • 17
  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) Please use quote format for only for quotes. Please try to be clear by using clear writing, not fonts. – philipxy Nov 30 '22 at 12:24
  • Debug questions require a [mre]. – philipxy Nov 30 '22 at 12:33
  • @philipxy Those are guidelines. When it comes to SQL tables such as questions like this, images are ok. – itachi Nov 30 '22 at 12:39
  • That [meta] image link is a SO/SE Inc [help] FAQ & its answers says it is not OK (and it says why). And the MRE link is a SO/SE Inc [help] page & the [ask] help pages say debug questions require a MRE. I'm done. – philipxy Nov 30 '22 at 12:41

1 Answers1

1

I achieved my goal by the following query:

SELECT 
    f1.id as f1_friendship_id, f1.follower_id as f1_user_id,
    u1.full_name as f1_full_name, u1.email as f1_email,
    f2.id as f2_friendship_id, f2.follower_id as f2_user_id,
    u2.full_name as f2_full_name, u2.email as f2_email
FROM 
    (friends f1 INNER JOIN users u1 ON f1.follower_id = u1.id ), 
    (friends f2 INNER JOIN users u2 ON f2.follower_id = u2.id)
WHERE 
    f1.follower_id = f2.following_id
    AND f1.following_id = f2.follower_id
    AND f2.id <> f1.id
    AND f1.status = 'A' AND f2.status = 'A';

In the above query, to segregate users those followed each other I used self-join and to append user info I used inner-join. I combined the joins.

Output:

users data those followed each other

philipxy
  • 14,867
  • 6
  • 39
  • 83
M-Khawar
  • 855
  • 5
  • 17
  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) – philipxy Nov 30 '22 at 12:28
  • @philipxy I understand what you want to say but it also transparent to you I have shown results in screenshot. if I starts to write those out result then I think i would not be able to convey my message properly to community. – M-Khawar Nov 30 '22 at 14:01