Trying to create a query that does the following:
A. Sorts all users in order of how many followers they have
B. Remove the users that the person making the request is already following
C. Remover the users that the person making the request has already seen
Here is my attempt:
SELECT COUNT( f.FOLLOWING_UID_FK ) AS Records , u.uid, u.full_name, u.bio, u.thumb_img, u.college, u.HEADER_IMG
FROM users AS u, FOLLOWERS AS f
WHERE u.uid <>1580
AND NOT u.uid IN ( SELECT UID_FK FROM FRIEND_FINDER_SWIPES WHERE UID_FK <>1580 )
AND NOT u.uid IN ( SELECT UID_FK FROM FOLLOWERS WHERE FOLLOWING_UID_FK <>1580 )
AND u.uid = f.FOLLOWING_UID_FK GROUP BY f.FOLLOWING_UID_FK ORDER BY records DESC LIMIT 0 , 30
Whats weird is this query works if broken up for example this will produce a list of all users in order of most followed :
SELECT COUNT( f.FOLLOWING_UID_FK ) AS Records, u.uid, u.full_name, u.bio, u.thumb_img, u.college, u.HEADER_IMG
FROM users AS u, FOLLOWERS AS f
WHERE u.uid <>1580
AND u.uid = f.FOLLOWING_UID_FK
GROUP BY f.FOLLOWING_UID_FK
ORDER BY records DESC
LIMIT 0 , 30
And this will return 30 users that the person making the request is not already following and has not seen already:
SELECT u.uid, u.full_name, u.bio, u.thumb_img, u.college, u.HEADER_IMG
FROM users AS u
WHERE u.uid <>1580
AND NOT u.uid IN ( SELECT UID_FK FROM FRIEND_FINDER_SWIPES WHERE UID_FK <>1580 )
AND NOT u.uid IN ( SELECT UID_FK FROM FOLLOWERS WHERE FOLLOWING_UID_FK <>1580 )
DESC LIMIT 0 , 30
But when you combine the 2 the results seem off to say the least. Why?
Your logic seems confused, as least relevant to the goals you have in mind.
First, you want to count the number of followers that users have. You can do that in a subquery. Then you want to remove people with a relationship to the current user. The
NOT IN . . . <>
has two double negatives. That doesn't seem right. I'm not quite sure what the direction is for the subqueries on theWHERE
clause; I'm leaving the logic pretty much as in your question.Also, learn to use proper
JOIN
syntax