SQLAlchemy query with OR/AND/like common filters

Posted 网名还没想好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLAlchemy query with OR/AND/like common filters相关的知识,希望对你有一定的参考价值。

http://www.leeladharan.com/sqlalchemy-query-with-or-and-like-common-filters

Some of the most common operators used in filter() method SQLAlchemy

equals:

query.filter(User.name == ‘leela‘)

not equals:

query.filter(User.name != ‘leela‘)

LIKE:

query.filter(User.name.like(‘%leela%‘))

IN:

query.filter(User.name.in_([‘leela‘, ‘akshay‘, ‘santanu‘]))

# works with query objects too:

query.filter(User.name.in_(session.query(User.name).filter(User.name.like(‘%santanu%‘))))

NOT IN:

query.filter(~User.name.in_([‘lee‘, ‘sonal‘, ‘akshay‘]))

IS NULL:

filter(User.name == None)

IS NOT NULL:

filter(User.name != None)

AND:

from sqlalchemy import and_
filter(and_(User.name == ‘leela‘, User.fullname == ‘leela dharan‘))

#or, default without and_ method comma separated list of conditions are AND

filter(User.name == ‘leela‘, User.fullname == ‘leela dharan‘)

# or call filter()/filter_by() multiple times

filter(User.name == ‘leela‘).filter(User.fullname == ‘leela dharan‘)

OR:

from sqlalchemy import or_
filter(or_(User.name == ‘leela‘, User.name == ‘akshay‘))

match:

query.filter(User.name.match(‘leela‘))

以上是关于SQLAlchemy query with OR/AND/like common filters的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别