between
datetime按天查询
Myclass.query.filter(myClass.date.between(_date, _date+timedelta(days = 1)))
filter 和 filter_by的区别
session.query(MyClass).filter(MyClass.name == 'some name')
session.query(MyClass).filter_by(name = 'some name')
filter 需要带上tablename,并用==
filter 多个条件的查询
AND操作
刚开始想用惯常的sql查询思维
tablename.filter(condition1 and condition2).all()
生成数据后发现condition2完全没有执行。
网上找了个折中的办法:
tablename.fiter(conditon1).filter(condition2).all()
虽然达到了想要的效果,但是有种隔靴搔痒的感觉。查找sqlachemy的代码,发现对于查询条件,是放在一个list中进行处理的。各个查询条件之间用‘,’分割:
tablename.filter(condition1,condition2).all
OR操作
from sqlachemy import or_
myclass.query.filter(or_(myclass.name=='xk', myclass.country=='china'))