如果表中关联表的列之一不满足条件,如何忽略表中的行?

发布于 2024-11-07 02:37:40 字数 798 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-11-14 02:37:40
SELECT * FROM Student s 
LEFT JOIN Course c ON s.id=c.student_id AND c.grade = 'C' 
WHERE c.student_id IS NULL;

或加入您的子查询

SELECT * FROM Student s 
LEFT JOIN (SELECT student_id FROM Course WHERE grade = 'C') c
WHERE c.student_id IS NULL;

或使用存在

SELECT * FROM Student s 
WHERE NOT EXISTS (SELECT NULL FROM Course WHERE grade = 'C' AND student_id=s.id);

恐怕我现在无法测试这一点,我怀疑这些查询可能对您没有帮助。我没有使用 ActiveRecord 的经验。请在评论中告诉我。

SELECT * FROM Student s 
LEFT JOIN Course c ON s.id=c.student_id AND c.grade = 'C' 
WHERE c.student_id IS NULL;

Or join to your subquery

SELECT * FROM Student s 
LEFT JOIN (SELECT student_id FROM Course WHERE grade = 'C') c
WHERE c.student_id IS NULL;

Or use exists

SELECT * FROM Student s 
WHERE NOT EXISTS (SELECT NULL FROM Course WHERE grade = 'C' AND student_id=s.id);

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.

少跟Wǒ拽 2024-11-14 02:37:40

甲骨文:

select distinct s.*  from student s left join course c on c.student_id=s.id where c.grade!='c'

Student.all(:select=>"distinct student.*",:joins=>"left join course on course.student_id=student.id",:conditions=>"course.grade!='c'")

oracle:

select distinct s.*  from student s left join course c on c.student_id=s.id where c.grade!='c'

Student.all(:select=>"distinct student.*",:joins=>"left join course on course.student_id=student.id",:conditions=>"course.grade!='c'")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文