python怎样实现多表连接查询_sqlalchemy多表联合查询

sqlalchemy多表联合查询(inner outer join 左右连接)详解

按用户名摸糊查询

trans_details.query.join(Uses).filter(Users.username.like('%xx%'))

#select xxx from trans_details inner join trans_details on users.id=trans_details.user_id where users.username like '%xx%'

左外联接(left join)

trans_details.query.outerjoin(Uses).filter(Users.username.like('%xx%'))

#select xxx from trans_details left outer join trans_details on users.id=trans_details.user_id where users.username like '%xx%'

以上是已经设置好外键,它自动找到关联的字段.也可以自己指定:

trans_details.query.join(Uses,trans_details.user_id==Users.id).filter(Users.username.like('%xx%'))

#select xxx from trans_details inner join trans_details on users.id=trans_details.user_id where users.username like '%xx%'

另外一个更复杂的例子:

q=db.session.query(Credit_bills_details.no,Credit_bills_details.amount,C

你可能感兴趣的:(python怎样实现多表连接查询_sqlalchemy多表联合查询)