尝试创建执行以下操作的查询:
一个。按照他们拥有多少粉丝的顺序对所有用户进行排序
B.删除发出请求的人已经关注的用户
C.将提出请求的人已经看到的用户卸下
以下是我的尝试:
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
奇怪的是这个查询有效,如果分解,例如这将按照大多数顺序生成所有用户的列表:
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
这将使30位用户返回发出请求的人尚未关注且尚未查看的内容:
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
但是当你结合2时,结果似乎至少可以说。为什么呢?
答案 0 :(得分:2)
你的逻辑似乎很困惑,与你想到的目标最不相关。
首先,您要计算用户拥有的关注者数量。您可以在子查询中执行此操作。然后,您要删除与当前用户有关系的人。 NOT IN . . . <>
有两个双重否定。这似乎不对。我不太清楚WHERE
子句中子查询的方向是什么;我在你的问题中完全抛弃逻辑。
另外,请学会使用正确的JOIN
语法
SELECT uf.NumFollowers, u.uid, u.full_name,
u.bio, u.thumb_img, u.college, u.HEADER_IMG
FROM (SELECT UID_FK, COUNT(*) as NumFollowers
FROM FOLLOWERS f
GROUP BY UID_FK
) uf JOIN
users u
ON uf.uid = u.FOLLOWING_UID_FK
WHERE u.uid <> 1580 AND
u.uid NOT IN (SELECT UID_FK FROM FRIEND_FINDER_SWIPES WHERE UID_FK = 1580 ) AND
u.uid NOT IN (SELECT UID_FK FROM FOLLOWERS WHERE FOLLOWING_UID_FK = 1580 )
GROUP BY u.uid
ORDER BY NumFollowers DESC
LIMIT 0, 30
答案 1 :(得分:0)
看来我回答了我自己的问题,我使用的是NOT而不是&lt;&gt; ANY,这就是我想出来的,它似乎正在起作用:
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 <> ANY(
SELECT UID_FK
FROM FRIEND_FINDER_SWIPES
WHERE UID_FK <>1580
)
AND u.uid <> ANY(
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