I am android developer and don't have much experience in mysql. I have 3 tables in mysql database.
First table :-UserTable
+-------------+-------------+----------+----------------------+
| user_id | name | image | joining_date |
+-------------+-------------+----------+----------------------+
| 100 | King | defualt.jpg | 2018-05-23 20:09:27 |
| 101 | Kochhar | defualt.jpg | 2018-05-23 20:09:27 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
Second Table :- friendRequests
+-------------+-------------+----------+
| unique_id | from_id | to_id |
+-------------+-------------+----------+
| 1 | 100 | 101 |
| 2 | 200 | 110 |
| - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - - - - -|
Third Table :- friends
+-------------+-------------+----------+
| unique_id | from_id | to_id |
+-------------+-------------+----------+
| 1 | 104 | 101 |
| 2 | 206 | 110 |
| - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - - - - -|
I want to get the list of users who are not friends to me and also we have not pending friend request. So i tried with following query:-
SELECT usertable.name,
usertable.user_id,
usertable.image
FROM `UserTable` AS usertable
WHERE usertable.user_id <> '100'
AND (SELECT Count(*)
FROM friendrequests
WHERE ( to_id = '100'
AND from_id = usertable.user_id )
OR ( to_id = usertable.user_id
AND from_id = '100' )) = 0
AND (SELECT Count(*)
FROM friends
WHERE to_id = usertable.user_id
AND from_id = '100'
OR to_id = '100'
AND from_id = usertable.user_id) = 0
ORDER BY usertable.joining_date DESC
LIMIT 10
It is working but takes 436 seconds. What is the proper way to write this query?