加入 3 个表并返回第二个和第三个中不存在的任何内容

我有 3 个表(用户、分配的技术人员、分配的主管)

http://img2.mukewang.com/63aeb3250001e54a05930114.jpg

technicalid 和 uspervisorid 是用户 ID 的外键 stationid 和 regionid 是一些其他表的外键

Essentialy 用户被分配给帖子,我想做的是输出未分配给帖子的用户,在这种情况下:

http://img3.mukewang.com/63aeb32f0001cc5f01630040.jpg

我知道完全外部连接必须是要走的路,但我无法让它工作



慕桂英4014372
浏览 71回答 3
3回答

慕容3067478

我会not exists为此使用两个条件,一个在每个网桥表中搜索:select u.*from userswhere    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)

九州编程

您可以像这样编写查询:SELECT * from users U where U.user_id NOT IN (SELECT technicianid FROM assignedtechnicians) AND U.user_id NOT IN (SELECT uspervisorid FROM assignedsupervisors);

喵喵时光机

您正在寻找LEFT JOIN以便您可以加入不存在的帖子(您正在寻找的帖子)SELECT users.*FROM usersLEFT JOIN posts ON posts.user_id = users.idWHERE posts.id IS NULLposts.id IS NULL表示用户没有任何帖子。
打开App,查看更多内容
随时随地看视频慕课网APP