MySQL查询尚未关注或已经刷过的所有用户

时间:2015-06-15 03:45:21

标签: mysql sql

所以我有下表

USERS 
user_id /name/bio

FOLLOWERS 
user_id_fk/following_uid_fk

SWIPED
user_id_fk/ swiped_on_uid_fk

我想查询用户尚未关注的所有用户,并且过去还没有刷过,我在查看它的用途时遇到了一些麻烦。

这是我的第一次尝试:

SELECT a.user_id, a.name
FROM users AS a, FOLLOWERS AS b, SWIPES AS c
WHERE a.user_id != b.swiped_on_user
  AND b.user_id_fk = '1'
  AND c.user_id_fk = '1'
  AND c.SWIPPED_user_id != a.user_id
LIMIT 0 , 30

1 个答案:

答案 0 :(得分:0)

我认为您需要这样的查询:

SELECT 
    u.user_id, u.name
FROM
    users As u
WHERE
    NOT u.user_id IN (
        SELECT
            user_id_fk
        FROM
            swiped)
    AND
    NOT u.user_id IN (
        SELECT
            user_id_fk
        FROM
            followers)
LIMIT 0, 30;