Python sqlalchemy 多表查询 没有外键
2015-12-08 15:37
543 查看
多表查询 没有外键
from sqlalchemy import and_
两张表
q = db.session.query(User, UserIn).join(UserIn, and_(User.id==UserIn.user_id, User.id == 4)).all()
三张表
q = db.session.query(User, UserIn, Supplier).join(UserIn, and_(User.id==UserIn.user_id, User.id == 4)).join(Supplier, and_(UserIn.supplier_id==Supplier.id)).all()
SELECT tb_supplier., tb_user., tb_user_belonging.* FROM tb_supplier, tb_user left JOIN tb_user_belonging ON tb_user.id = tb_user_belonging.user_id AND tb_user_belonging.supplier_id = tb_supplier.id AND tb_user.id = 4
from sqlalchemy import and_
两张表
q = db.session.query(User, UserIn).join(UserIn, and_(User.id==UserIn.user_id, User.id == 4)).all()
三张表
q = db.session.query(User, UserIn, Supplier).join(UserIn, and_(User.id==UserIn.user_id, User.id == 4)).join(Supplier, and_(UserIn.supplier_id==Supplier.id)).all()
SELECT tb_supplier., tb_user., tb_user_belonging.* FROM tb_supplier, tb_user left JOIN tb_user_belonging ON tb_user.id = tb_user_belonging.user_id AND tb_user_belonging.supplier_id = tb_supplier.id AND tb_user.id = 4
相关文章推荐
- 简单总结Python中几种字符串连接的方法
- Python实现二叉树遍历方法(leetcode)
- Python中 os.system()与os.popen()
- 【原】python中文文本挖掘资料集合
- Python+Hadoop Streaming实现MapReduce(word count)
- python开发_常用的python模块及安装方法
- Python
- python --装饰器
- python
- Python yield
- python 学习笔记一
- Python使用pygame模块编写俄罗斯方块游戏的代码实例
- Python 模版(一)
- python基础之模块
- python(1)介绍和流程控制
- Python元组tuple
- python之Wing IDE中文乱码问题
- Python编程预约参观北京行动纲要
- python numpy使用
- PyQt5系列教程(一)Mac OS X下搭建Python3.5.1+PyQt5开发环境