res = session.query(User).all() # 是个普通列表
print(type(res))
print(len(res))
all()的结果就是列表,列表里面是对象
# select name as xx,email from user;
res = session.query(User.name.label('xx'), User.email) #没有all(),打印是原生sql语句
#label是起别名
res = session.query(User).filter(User.name == "lqz").all()
res = session.query(User).filter(User.name != "lqz").all()
res = session.query(User).filter(User.name != "lqz", User.email == '[email protected]').all()
res = session.query(User).first()
all()之后就不能.first()
# select * from user where id <20 or name=lqz
res = session.query(User).filter(text("id<:value or name=:name"))
.params(value=20, name='lqz').all()
from_statement 写纯原生sql
res=session.query(User).from_statement(text("SELECT * FROM users where email=:email"))
.params(email='[email protected]').all()
1、and条件连接 ---------,就是and
res = session.query(User).
filter(User.id > 1, User.name == 'lqz099').all() # and条件
2、between ------在什么之间
res = session.query(User).filter(User.id.between(1, 9), User.name == 'lqz099').all()
res = session.query(User).filter(User.id.between(1, 9)).all()
3、in_ ----------是否是1,2,4
res = session.query(User).filter(User.id.in_([1,3,4])).all()
res = session.query(User).filter(User.email.in_(['[email protected]','[email protected]'])).all()
4、 ~非,除。。外
res = session.query(User).filter(~User.id.in_([1,3,4])).all()
id不是1,或3,或4,的所有
5. 二次筛选
res = session.query(User).filter(~User.id.in_(session.query(User.id).
filter_by(name='lqz'))).all()
6、and or条件
from sqlalchemy import and_, or_
# or_包裹的都是or条件,and_包裹的都是and条件
res = session.query(User).filter(and_(User.id >= 3, User.name == 'lqz099')).all() # and条件
res = session.query(User).filter(User.id < 3, User.name == 'lqz099').all() # 等同于上面
res = session.query(User).filter(or_(User.id < 2, User.name == 'eric')).all()
res = session.query(User).filter(or_(User.id < 2,and_(User.name == 'lqz099', User.id > 3),User.extra != ""))
7、通配符,以e开头,不以e开头
res = session.query(User).filter(User.email.like('%@%')).all()
# select user.id from user where user.name not like e%;
res = session.query(User.id).filter(~User.name.like('e%'))
res = session.query(User).filter(~User.name.like('e%')).all()
8、分页
# 一页2条,查第5页
res = session.query(User)[2*5:2*5+2]
9、排序,根据name降序排列(从大到小)
res = session.query(User).order_by(User.email.desc()).all()
res = session.query(Book).order_by(Book.price.desc()).all()
res = session.query(Book).order_by(Book.price.asc()).all()
# 第一个条件重复后,再按第二个条件升序排
res = session.query(User).order_by(User.name.desc(), User.id.asc())
10、分组查询 5个聚合函数
from sqlalchemy.sql import func
# 分组后,只能拿分组字段和聚合函数字典,如果拿别的,是严格模式,会报错
res = session.query(User).group_by(User.extra) # 如果是严格模式,就报错
# 分组之后取最大id,id之和,最小id 和分组的字段
from sqlalchemy.sql import func
res = session.query(
User.name,
func.max(User.id),
func.sum(User.id),
func.min(User.id),
func.avg(User.id)).group_by(User.name).all()
for item in res:
print(item)
# 分组后having
# select name,max(id),sum(id),min(id) from user group by user.name having id_max>2;
from sqlalchemy.sql import func
res = session.query(
User.name,
func.max(User.id),
func.sum(User.id),
func.min(User.id)).group_by(User.name).having(func.max(User.id) > 2).all()
print(res)
### 关联关系,基于连表的跨表查询
from models1 import Person,Hobby
# 链表操作
select * from person,hobby where person.hobby_id=hobby.id;
res = session.query(Person, Hobby).filter(Person.hobby_id == Hobby.id).all()
# 自己连表查询
# join表,默认是inner join,自动按外键关联
# select * from Person inner join Hobby on Person.hobby_id=Hobby.id;
res = session.query(Person).join(Hobby).all()
#isouter=True 外连,表示Person left join Favor,没有右连接,反过来即可
# select * from Person left join Hobby on Person.hobby_id=Hobby.id;
res = session.query(Person).join(Hobby, isouter=True).all()
# 没有right join,通过这个实现
res = session.query(Hobby).join(Person, isouter=True).all()
# # 自己指定on条件(连表条件),第二个参数,支持on多个条件,用and_,同上
# select * from Person left join Hobby on Person.id=Hobby.id;
res = session.query(Person).join(Hobby, Person.hobby_id == Hobby.id, isouter=True) # sql本身有问题,只是给你讲, 自己指定链接字段
# 右链接
# print(res)
# 多对多关系连表
# 多对多关系,基于链表的跨表查
# 多表链接
#方式一:直接连
#select * FROM boy, girl, boy2girl WHERE boy.id = boy2girl.boy_id AND girl.id = boy2girl.girl_id
res = session.query(Boy, Girl,Boy2Girl).filter(Boy.id == Boy2Girl.boy_id,Girl.id == Boy2Girl.girl_id)
# 方式二:join连
# SELECT* FROM boy INNER JOIN boy2girl ON boy.id = boy2girl.boy_id INNER JOIN girl ON girl.id = boy2girl.girl_id WHERE boy.id >= %(id_1)s
res = session.query(Boy).join(Boy2Girl).join(Girl).filter(Boy.id>=2)
print(res)