sqlalchemy时间过滤How to filter date field?

sqlalchemy

查找时间段

db.query(User).filter(and_(User.birthday <= ‘2020-01-17’, User.birthday >= ‘2015-01-17’))

也可以利用between

db.query(User).filter(User.birthday.between(‘1985-01-17’, ‘1988-01-17’))

使用func.date

from sqlalchemy import and_, func

query = DBSession.query(User).filter(and_(func.date(User.birthday) >= ‘1985-01-17’),func.date(User.birthday) <= ‘1988-01-17’))
查询的范围是 1985-01-17 00:00 - 1988-01-17 23:59

小伟科技 wechat
python爱好者公众号—每日学习python必备
欢迎打赏支持!