1

Hi I am trying to match 2 rows from same table? For example below are the 2 rows of a single table. I wan to know if the primaryuser has liked the likeduser and the same liked user under primary user liked the primary user. For example

primaryuser   LikedUser   Likes
101           102         Yes
102           101         Yes
103           102         No
104           301         Yes

Table

O/P:
101, 102

here when 101 was primary user they liked user with ID 102 and when 102 was primary user they liked user with ID 101 so they have mutually liked each other.

CompGeek
  • 11
  • 2

1 Answers1

0

Something like this should work:

SELECT A.primaryUser, B.primaryUser
  FROM likesTable A, likesTable B
 WHERE A.primaryUser = B.likedUser
   AND A.likedUser = B.primaryUser
   AND A.liked = 'Yes'
   AND B.liked = 'Yes';
  • Realize that will give you 2 results for every math (e.g. "101, 102") as well as "102, 101". You can fix this by adding an additional WHERE condition that ```A.primaryUser < B.primaryUser```. – Vinnie Pescado Aug 13 '18 at 17:12
  • I did not get the desired output from this. the data is from the same table. Take "persons" as table name. – CompGeek Aug 13 '18 at 18:30
  • What output did you get, and what was wrong with it? If the table name is ```persons```, then change the second line to ```FROM persons A, persons B``` – Vinnie Pescado Aug 13 '18 at 21:30
  • Oh I got it. The ID's were repeating. Now i included your comment statement. Thank you! – CompGeek Aug 13 '18 at 23:16