如果表中关联表的列之一不满足条件,如何忽略表中的行?
我正在尝试在 ruby on Rails 中执行以下操作,但即使我可以在 mysql 中得到答案,那就太好了。
我有学生表和课程表。学生->课程是一对多的关系。
Student Course |--------------| |--------------------------------------| |id| name | |id | course_name | grade | student_id | |---------- | |--------------------------------------| |S1| student 1 | |C1 | Course1 | A | S1 | |S2| student 2 | |C2 | Course2 | C | S1 | |---------- | |C3 | Course1 | A | S2 | |C4 | Course2 | B | S2 | |--------------------------------------|
select * from Student
where id NOT IN (select student_id from Course where grade = 'C')
我想使用单个 SQL JOIN 语句或使用 activerecord 获得相同的结果。
I am trying to do the following in ruby on rails, but even if I can get an answer in mysql that would be great.
I have tables Student and Courses. Student -> Courses is one-to-many relationship.
Student Course |--------------| |--------------------------------------| |id| name | |id | course_name | grade | student_id | |---------- | |--------------------------------------| |S1| student 1 | |C1 | Course1 | A | S1 | |S2| student 2 | |C2 | Course2 | C | S1 | |---------- | |C3 | Course1 | A | S2 | |C4 | Course2 | B | S2 | |--------------------------------------|
select * from Student
where id NOT IN (select student_id from Course where grade = 'C')
I want to achieve same result using single SQL JOIN statement or using activerecord.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或加入您的子查询
或使用存在
恐怕我现在无法测试这一点,我怀疑这些查询可能对您没有帮助。我没有使用 ActiveRecord 的经验。请在评论中告诉我。
Or join to your subquery
Or use exists
I'm afraid I can't test this at the moment and I have a suspicion that these queries may not help you. I have no experience with ActiveRecord. Let me know in the comments.
甲骨文:
oracle: