1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| session.query(User).all()
session.query(User).filter(User.name == "iFan").all() session.query(User).filter(User.name != "iFan").all() session.query(User).filter(User.name.like("%ifan%")).all() session.query(User).filter(User.name.ilike("%ifan%")).all() session.query(User).filter(User.name.in_(["ifan", "ifan1"])).all() session.query(User).filter(User.name.in_(session.query(User.name).filter(User.name.like("%ifan%")))).all()
from sqlalchemy import tuple_ query.filter(tuple_(User.name, User.nickname).in_([('ed', 'edsnickname'), ('wendy', 'windy')]))
session.query.filter(~User.name.in_(['ed', 'wendy', 'jack']))
session.query(User).filter(User.name.is_not(None)).all()
from sqlalchemy import and_ session.query.filter(and_(User.name == 'ed', User.fullname == 'Ed Jones'))
from sqlalchemy import or_ session.query.filter(or_(User.name == 'ed', User.name == 'wendy'))
session.query.filter(User.id == 1).one_or_none()
from sqlalchemy import text session.query.filter(text("id = 1")).order_by(text("id").all() session.query(User).filter(text("id<:value and name=:name")).params(value=224, name='fred').order_by(User.id).one() session.query(User).from_statement(text("SELECT * FROM users where name=:name")).params(name='ed').all() stmt = text("SELECT name, id, fullname, nickname FROM users where name=:name") stmt = stmt.columns(User.name, User.id, User.fullname, User.nickname) session.query(User).from_statement(stmt).params(name='ed').all()
for user in session.query(User).filter(User.age == 18)[:18]: print(user.username, user.age)
|