Table Interest_log
Id user_id user_id_interested
------ ------- ------------------
3 2 3
5 3 2
6 6 2
7 6 3
8 7 2
9 2 6
I'm selecting those users where user_id_interested =2 very simple select a.user_id ,a.user_id_interested from interest_log a where a.user_id =2;
when I select those users where user_id_interested = 2 and in new column I select those user_id who also liked back to the user who like him.
Expecting result
Id user_id user_id_interested bUser_id
------ ------- ------------------ -----------
5 3 2 null
6 6 2 2
8 7 2 null
This is about who like user 2 and In bUserId, first row and third row has null while second row has value 2 because user_id 2 also liked user_id 6 but user 2 didn't like user 3, 7 so there is null
I wrote this query
SELECT a.user_id ,a.user_id_interested , b.user_id AS bUserId
FROM interest_log a
LEFT OUTER JOIN interest_log b
ON a.user_id_interested = b.user_id
WHERE a.user_id_interested =2
AND b.user_id = a.user_id_interested
but it's giving following result
user_id user_id_interested bUserId
------- ------------------ ---------
3 2 2
3 2 2
6 2 2
6 2 2
any idea what join should be used here. i'm really thankful if someone can tell what join should be used and how to write tat join it in cakephp 3 .