简体   繁体   English

从 INNER JOIN 查询返回元组的逆

[英]Return the Inverse of Tuples from an INNER JOIN Query

I am trying to get the remaining tuples from a query.我正在尝试从查询中获取剩余的元组。 In this case, course_id 3 and 4 from courses because the user admin@ has NOT taken these (they've only taken 1 and 2).在这种情况下, course_id 3 和 4 来自课程,因为用户 admin@ 没有使用这些(他们只使用了 1 和 2)。 The tables are already joined nicely and queries are working when I try a LEFT JOIN...当我尝试 LEFT JOIN 时,这些表已经很好地连接并且查询正在工作......

select course_name from courses LEFT OUTER JOIN users_courses ON users_courses.user_course_id = courses.course_id where users_courses.user_email = "admin@---.com";

But this returns the courses 1 and 2, not 3 and 4但这会返回课程 1 和 2,而不是 3 和 4

在此处输入图像描述

you could do another thing by getting all of the courses that is not exists in the courses list for this user like this您可以通过像这样获取该用户的课程列表中不存在的所有课程来做另一件事

select course_name from courses where course_id not in (select user_course_id from users_courses where user_email = 'admin@....')

update更新

forget to add the column name course_id忘记添加列名course_id

Ah.啊。 I misunderstood what you were after, Joseph's method will work, but, as a rule.我误解了你所追求的,约瑟夫的方法会奏效,但是,作为一项规则。 you should try to avoid "in(select...".你应该尽量避免“in(select ...”)。

To knock-out the courses the admin has taken, just add a null condition:要淘汰管理员所学的课程,只需添加 null 条件:

select course_name from courses LEFT OUTER JOIN users_courses
ON users_courses.user_course_id = courses.course_id
and users_courses.user_email = "admin@---.com"
where users_courses.course_id is null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM