SQLAlchemy:同时从多个链接表中提取多个字段

时间:2018-11-16 16:31:39

标签: python sqlalchemy

我有四个数据库表。附带说明,这些表不能更改。我试图在此举例说明我的问题:

table1: (describes person's name)
id (primary key)
person_name (a string)

table 2: (describes a list of diseases)
id (primary key)
disease (a string)
...some other variables...

table 3 (links the person with a disease):
id (primary key)
table1_id_id
table2_id_id
….some other variables....


table 4 (links table 1 and table 3; with other info):
table3_id_id
table1_id_id
date_of_birth

我有一个名字列表: ['Ann','Barry','Tom']

我想拿出他们的姓名,疾病,出生日期(即将信息合并到多个表格中)。

我写了这段代码:

Table1 = tbs['Names']
Table2 = tbs['Diseases']
Table3 = tbs['NamesAndDiseasesLinked']
Table4 = tbs['NamesDiseasesAndOtherInfoLinked']
name_list = ['Ann','Barry','Tom']


data = session.query(Table4,Table1).join(Table1).all()
for i in name_list: 
    for Table1,Table4 in data:
        if Table1.person_name == i:
            print Table4. #??????

因此最终输出将类似于:

Ann    Diabetes      25-9-65
Barry  Hypertension  6-6-81
Tom    Influenza     6-9-75

我试图遵循thisthis之类的答案,但是努力将其应用于我的示例吗?

谢谢

0 个答案:

没有答案
相关问题