3

我有以下代码

#!/usr/bin/env python

"""doc"""

import peewee

db = peewee.SqliteDatabase(":memory:")


class BaseModel(peewee.Model):  # pylint: disable=W0232
    """base model"""

    class Meta:  # pylint: disable=C0111,W0232,R0903
        database = db


class Student(BaseModel):
    """doc"""
    name = peewee.CharField()


class Course(BaseModel):
    """doc"""
    name = peewee.CharField()


class StudentCourse(BaseModel):
    """doc"""
    student = peewee.ForeignKeyField(Student)
    course = peewee.ForeignKeyField(Course)

Student.create_table()
Course.create_table()
StudentCourse.create_table()

s1 = Student(name="Student1")
s1.save()
s2 = Student(name="Student2")
s2.save()
s3 = Student(name="Student3")
s3.save()
s4 = Student(name="Student4")
s4.save()
c1 = Course(name="course1")
c1.save()
c2 = Course(name="course2")
c2.save()
c3 = Course(name="course3")
c3.save()
sc21 = StudentCourse(student=s2, course=c1)
sc21.save()
sc22 = StudentCourse(student=s2, course=c2)
sc22.save()
sc23 = StudentCourse(student=s2, course=c3)
sc23.save()
sc31 = StudentCourse(student=s3, course=c1)
sc31.save()
sc32 = StudentCourse(student=s3, course=c2)
sc32.save()
sc41 = StudentCourse(student=s4, course=c1)
sc41.save()

query = (
    StudentCourse
    .select(StudentCourse, Student, Course)
    .join(Course)
    .switch(StudentCourse)
    .join(Student)
    .order_by(Student.name))

last = None
for student_course in query:
    student = student_course.student
    if student != last:
        last = student
        print "Student: %s" % student.name
    print "    - %s" % student_course.course.name

产生这个输出:

Student: Student2
    - course1
    - course2
    - course3
Student: Student3
    - course1
    - course2
Student: Student4
    - course1

这基本上是来自peewee docs的示例

我需要过滤结果并仅选择那些拥有给定子集中所有课程的学生:

courses = ["course1", "course2"]

我尝试使用IN运算符并修改选择查询,如下所示:

    query = (
    StudentCourse
    .select(StudentCourse, Student, Course)
    .join(Course)
    .switch(StudentCourse)
    .join(Student)
    .where(Course.name << courses)
    .order_by(Student.name))

但它会产生错误的结果,这意味着我做错了:

Student: Student2
    - course1
    - course2
Student: Student3
    - course1
    - course2
Student: Student4
    - course1

这里有两个错误的地方:

  1. Student2course3没有打印,我需要它。我认为这是因为course3不在课程列表中。
  2. Student4被选中,但它不应该被选中,因为Student4没有course1course2,但course1课程列表中。

是否有可能在一个查询中实现我想要的,或者我必须遍历学生,为每个学生进行SELECT查询,并将他们的课程列表与我的课程过滤器相匹配?

我的目标是得到这个输出:

Student: Student2
    - course1
    - course2
    - course3
Student: Student3
    - course1
    - course2
4

1 回答 1

-1

这应该让您指出正确的方向:https ://www.simple-talk.com/sql/t-sql-programming/divided-we-stand-the-sql-of-relational-division/

于 2013-11-27T14:50:29.393 回答