I have complex SQL QUERY I need some Help for the following conditions:
I'm looking for an SQL query with the below conditions:
SELECT * FROM Table_1,Table_2 where user_id = ’16’ // 16 = ($_SESSION[ 'SESS_USER_ID' ])
- CONDITION 1 -> DON’T SHOW MYSELF (CURRENT login user_id. => 16 (IT’s ME) ($_SESSION[ 'SESS_USER_ID' ]))
- CONDITION 2 -> DON’T SHOW user_id 25, 26, 27, 28 in the list because they are already in table_2 for with some conditions for user_id =’16’ and CONDITION 0,1 from friend_status
- CONDITION 3 -> ORDER BY signupdate
Sample of the result I am expecting below: WHAT I NEED-> I need the results showing all the members for login 'user_id' that are not in Table_2 'friend_status' CONDITION (0,1,2)
In the below table results, you can see that login 'user_id' (16 in this example) cannot see other 'user_id' that are in Table_2 CONDITION (0,1,2) including himself as the login 'user_id' (16)
//Perfect SQL results for me.
+—————————+—————————————+
+ user_id +signupdate +
+—————————+—————————————+
+ 23 + 2020-11-30 +
+—————————+—————————————+
+ 24 + 2020-11-30 +
+—————————+—————————————+
+ 28 + 2020-11-30 +
+—————————+—————————————+
+ 21 + 2020-11-30 +
+—————————+—————————————+
WHAT I NEED -> I need the an SQL QUERY to show results showing all the members for login 'user_id'(16) that are not in Table_2 'friend_status' CONDITION (0,1,2)
PROBLEM-> The Results I am getting below don't match the requested 'friend_status' CONDITION (0,1,2) of Table_2.
WHY? Because login 'user_id' should no see all the 'friend_status' CONDITION (0,1,2) in the full members page results that have NO CONDITION (0,1,2)
WHY? Because These SQl results CONDITION (0,1,2) are displayed elsewhere in 3 differents results pages for each CONDITION (0,1,2) -> Not todays topic.
//Wrong SQL results for me.
+—————————+—————————————+
+ user_id +signupdate
+—————————+—————————————+
+ 16 + 2020-11-30 + <— SHOULD DO NOT SHOW, CONDITION-> ITS ME “login” $user_id = ’16’
+—————————+—————————————+
+ 23 + 2020-11-30 +
+—————————+—————————————+
+ 24 + 2020-11-30 +
+—————————+—————————————+
+ 25 + 2020-11-30 +<— SHOULD NOT SHOW -> TABLE_2 CONDITION user_id = ( $_SESSION[ 'SESS_USER_ID' ])=’16’ AND CONDITION2 friend_status= ‘0’(Pending approval Mutual friends)
+—————————+—————————————+
+ 26 + 2020-11-30 +<— SHOULD NOT SHOW -> TABLE_2 CONDITION user_id = ( $_SESSION[ 'SESS_USER_ID' ])=’16’ AND CONDITION2 friend_status= ‘0’ (Pending approval Mutual friends)
+—————————+—————————————+
+ 27 + 2020-11-30 +<— SHOULD NOT SHOW -> TABLE_2 CONDITION user_id = ( $_SESSION[ 'SESS_USER_ID' ])=’16’ AND CONDITION2 friend_status= ‘1’ (Mutual friends)
+—————————+—————————————+
+ 28 + 2020-11-30 +<— SHOULD NOT SHOW -> TABLE_2 CONDITION user_id = ( $_SESSION[ 'SESS_USER_ID' ])=’16’ AND CONDITION2 friend_status= ‘2’ (Blocked)
+—————————+—————————————+
+ 21 + 2020-11-30 +
+—————————+—————————————+
TABLE_2 (table for friends relationship) friend_status=‘0’ -> This means 'user_id' pending friendship acceptance by 'user_id_resquest' -> Do not Show in full members list for login 'user_id' friend_status=‘1’ -> This 'user_id' and 'user_id_resquest' are already mutual friends. -> Do not Show in full members list for login user_id friend_status=‘2’ -> This means 'user_id' is blocked by 'user_id_resquest' -> Do not Show in members list for current login user_id
+——————————————+—————————+——————————————+—————————————————+
+friendship_id + user_id + user_id_request + friend_status+
+——————————————+—————————+——————————————+—————————————————+
+ 1 + 16 + 26 + 0 +//0->Pending
+——————————————+—————————+——————————————+—————————————————+
+ 2 + 16 + 25 + 0 +//0->Pending
+——————————————+—————————+——————————————+—————————————————+
+ 3 + 16 + 27 + 1 +//1->Mutual
+——————————————+—————————+——————————————+—————————————————+
+ 4 + 21 + 27 + 1 +//1->Mutual
+——————————————+—————————+——————————————+—————————————————+
+ 5 + 16 + 28 + 2 +//2 ->blocked
+——————————————+—————————+——————————————+—————————————————+
TABLE_1 (table of all the users)
+—————————+—————————————+
+ user_id + signupdate +
+—————————+—————————————+
+ 16 + 2020-11-30 +
+—————————+—————————————+
+ 23 + 2020-11-30 +
+—————————+—————————————+
+ 24 + 2020-11-30 +
+—————————+—————————————+
+ 25 + 2020-11-30 +
+—————————+—————————————+
+ 26 + 2020-11-30 +
+—————————+—————————————+
+ 27 + 2020-11-30 +
+—————————+—————————————+
+ 28 + 2020-11-30 +
+—————————+—————————————+