# 查询更多操作 from create_table import User, engine from sqlalchemy.orm import sessionmaker Session = sessionmaker(engine) db_session = Session() # 表达式!!! # res = db_session.query(User).filter(User.id==4 and User.name =='XJ3').all() # print(res[0].name) # 使用sqlalchemy 自带的and_ or_ from sqlalchemy.sql import and_, or_ ret = db_session.query(User).filter(and_(User.id == 4, User.name=='XJ3')).all() print(ret) ret2 = db_session.query(User).filter(or_(User.id == 1, User.name=='XJ3')).all() print(ret2) # 只查询id, 起别名 r2 = db_session.query(User.id.label('uid'), User.name).first() print(r2.uid, r2.name) # 字符串匹配方式筛选条件 from sqlalchemy.sql import text r3 = db_session.query(User).filter(text('id=:value and name=:name')).params(value=4, name='XJ3') # 排序 # 正序 user_list = db_session.query(User).order_by(User.id).all() # 倒序 user_list2 = db_session.query(User).order_by(User.id.desc()).all()