我想列出$_SESSION['userid']
关注的所有人,而不是
以下查询适用于跟随演员,它可以正常工作
IN (慢)
SELECT user.* FROM user
WHERE user.userid
IN (SELECT follow.followtoid FROM follow
WHERE follow.followerid = $_SESSION['userid'])
加入(快速)
SELECT p.*
FROM user p
JOIN follow f ON p.userid = f.followtoid
WHERE f.followerid = $_SESSION['userid']
对于那些仍然没有跟随演员的人来说,它有效但似乎很慢 请建议我如何使用JOIN代替NOT IN
NOT IN (慢)
SELECT user.* FROM user
WHERE user.userid
NOT IN (SELECT follow.followtoid FROM follow
WHERE follow.followerid = $_SESSION['userid'])
答案 0 :(得分:3)
您可以使用LEFT JOIN
并删除所有匹配的行;
SELECT p.*
FROM user p
LEFT JOIN follow f
ON p.userid = f.followtoid
AND f.followerid = $_SESSION['userid']
WHERE f.followtoid IS NULL
答案 1 :(得分:1)
试试这个:
SELECT u.*
FROM user u
LEFT JOIN follow f ON u.userid = f.followtoid AND f.followerid = $_SESSION['userid']
WHERE f.followtoid IS NULL;
或强>
SELECT u.*
FROM user u
WHERE NOT EXISTS (SELECT 1 FROM follow f
WHERE u.userid = f.followtoid AND f.followerid = $_SESSION['userid']
);