Visual Studio 2013进行Python开发(三)--多表联合查询
话不多说,接上一篇文章,直接在views.py中添加如下代码:
@app.route("/Query") # 上文的写法 def GetUserList(): #def __init__(self): engine = create_engine("mssql+pymssql://sa:123456@127.0.0.1/User?charset=utf8",echo=True,encoding='utf-8',convert_unicode=True) DBsession = sessionmaker(bind=engine) #创建DBsession类 session = DBsession() #创建对象 query = session.query(models.User,models.UserOutRightRelation,models.OutRight) query = query.join( # 取出所有 models.User, models.User.RelationGUID == models.UserOutRightRelation.UserGUID).filter(models.User.Telephone=='15209212730').join(models.OutRight,models.OutRight.RelationGUID==models.UserOutRightRelation.RightGUID) .with_entities(models.User.LoginID,models.User.Name,models.UserOutRightRelation.RightGUID,models.OutRight.RightName); data = query.all() data_list = [dict(zip(result.keys(), result)) for result in data] return jsonify({'message': data_list})
filter:过滤条件,
with_entities:显示哪些字段