当表在Sqlalchemy上连接时,是否应该指定所有键名

2024-05-13 06:56:18 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是示例表:

1学生
studentID—主键
学生姓名
大学ID

2大学
学院ID—主键
学院名称

我想获得所有学生的关键价值观和所有大学的关键价值观

下面是sqlalchemy查询的示例。你知道吗

from example.database import dao
    ...
    ...
    example = dao.query(students).\
          join(colleges, 
               students.collegeID == colleges.collegeID).\
          all()

但它没有collegeName数据。只有学生数据。
所以我不得不改变它。你知道吗

example = dao.query(students.studentID, 
                students.studentName,
                stduents.collegeID,
                colleges.collegeName).\
          join(colleges, students.collegeID == colleges.collegeID).\
          all()

如何在不指定所有所需密钥的情况下更轻松、更快地执行此操作。你知道吗


Tags: id示例examplequery学生关键大学学院
1条回答
网友
1楼 · 发布于 2024-05-13 06:56:18
example = (dao.query(students, colleges)
    .join(colleges, students.collegeID == colleges.collegeID)
    ).all()

for student, college in example:
    print(student, college)

这将返回元组值列表:

 <student-1, college-1>
 <student-1, college-2>
 ...
 <student-N, college-?>

相关问题 更多 >