SQLAlchemy操作MariaDB笔记之五

多表查询

手动连接

for uname,email_address in session.query(User.fullname,Address.email_address).filter(User.id==Address.user_id,User.name=="jack"): 
    print uname
    print email_address 

自然连接

>>> session.query(User).join(Address).\
...         filter(Address.email_address=='[email protected]').\
...         all() 
[<User(name='jack', fullname='Jack Bean', password='gjffdd')>]

指定连接方式

query.join(Address, User.id==Address.user_id)    # explicit condition
query.join(User.addresses)                       # specify relationship from left to right
query.join(Address, User.addresses)              # same, with explicit target
query.join('addresses') # same, using a string

外连接

query.outerjoin(User.addresses)   # LEFT OUTER JOIN

你可能感兴趣的:(DB,maria)