---
title: "flask-sqlachemy note"
author: "essen"
date: "2012-12-10T17:15:10Z"
tags: []
categories: ["编程语言"]
slug: "flask-sqlachemy-note"
draft: false
lastmod: "2012-12-24T14:09:19Z"
id: 1745149
url: "https://essen.iteye.com/blog/1745149"
---

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,并用==

[http://docs.sqlalchemy.org/en/rel\_0\_7/orm/query.html?highlight=filter\_by#sqlalchemy.orm.query.Query.filter](http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html?highlight=filter_by#sqlalchemy.orm.query.Query.filter)

 

 

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'))
```