我正在尝试根据以下条件过滤结果以显示我:我会写下我正在尝试的内容:
我有4个表:users,rp,rpa,rpud - 所有表都有rfID作为Key,这是用户表中的PK
现在我正在尝试的是:会员资格= 1是基本会员资格
这是我正在尝试的,这似乎是错误的:
SELECT
id,firstname,lastname, membership
FROM
users
where membership = 1
and users.rfID not in (select rfID from rp) or users.rfID not in (select rfID from rpa)
and count(rpud.rfID) <= 3
答案 0 :(得分:0)
首先,您需要将逻辑从or
更改为and
。你希望所有这些条件都成立。
其次,您不能提及表名,除非您首先在from
子句中声明它。以下应该做你想要的:
SELECT id, firstname, lastname, membership
FROM users u
where u.membership = 1 and
u.rfID not in (select rfID from rp) and
u.rfID not in (select rfID from rpa) and
u.rfId not in (select rfID from rpud group by rfid having count(*) > 3);
编辑:
要提高效果,请尝试将这些更改为join
s:
SELECT id, firstname, lastname, membership
FROM users u left join
rp
on rp.rfID = u.rfID left join
rpa
on rpa.rfID = u.rfID left join
(select rfID
from rpud
group by rfid
having count(*) > 3
) rpud
on rpud.rfID = u.rfId
where u.membership = 1 and rp.rfID is null and rpa.rfID is null and rpud.rfID is null;