我有 3 个表(用户、分配的技术人员、分配的主管)technicalid 和 uspervisorid 是用户 ID 的外键 stationid 和 regionid 是一些其他表的外键Essentialy 用户被分配给帖子,我想做的是输出未分配给帖子的用户,在这种情况下:我知道完全外部连接必须是要走的路,但我无法让它工作
3 回答

慕容3067478
TA贡献1773条经验 获得超3个赞
我会not exists为此使用两个条件,一个在每个网桥表中搜索:
select u.*
from users
where
not exists (select 1 from assignedtechnicians ast where ast.technicianid = u.id)
and not exists (select 1 from assignedsupervisors ass where ass.supervisorid = u.id)

九州编程
TA贡献1785条经验 获得超4个赞
您可以像这样编写查询:
SELECT * from users U where U.user_id NOT IN (SELECT technicianid FROM assignedtechnicians) AND U.user_id NOT IN (SELECT uspervisorid FROM assignedsupervisors);

喵喵时光机
TA贡献1846条经验 获得超7个赞
您正在寻找LEFT JOIN以便您可以加入不存在的帖子(您正在寻找的帖子)
SELECT users.*
FROM users
LEFT JOIN posts ON posts.user_id = users.id
WHERE posts.id IS NULL
posts.id IS NULL表示用户没有任何帖子。
- 3 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消