[英]Join 2 tables based on a single table
I can't seem to get the join / query that I need! 我似乎无法获得所需的联接/查询!
Lets say I have 3 tables (trimmed for this post...) 可以说我有3张桌子(为此帖子修剪了……)
user_courses
(int) user_id
(int) course_id
users
(int) user_id
(txt) name
(txt) access
courses
(int) course_id
(txt) course_desc
What I am trying to do is select select all users with a certain access type that are taking a specific course (course_id) 我想做的是选择选择所有具有特定访问权限的用户并且正在学习特定课程(course_id)的用户
something like... 就像是...
SELECT *
FROM user_courses uc
JOIN users u
ON uc.user_id = u.user_id
JOIN courses c
ON uc.course_id = c.course_id
WHERE u.access = "foobar"
... but working like I want it to :) I can get close, but will have extra users that don't have the correct access type. ...但是可以像我希望的那样工作:)我可以接近,但是会有更多没有正确访问类型的用户。
Use inner join
. 使用
inner join
。
SELECT *
FROM user_courses uc
INNER JOIN users u
ON uc.user_id = u.user_id
LEFT JOIN courses c
ON uc.course_id = c.course_id
WHERE u.access = "foobar"
perhaps: 也许:
select * from users u
where u.access='foobar'
and exists (select 1 from user_courses uc where uc.user_id=u.user_id)
Cheers 干杯
Try 尝试
...
WHERE ISNULL(u.access, '') = 'foobar'
Not sure if your 'access' field can be null? 不确定您的“访问”字段是否可以为空?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.