码迷,mamicode.com
首页 > 数据库 > 详细

SQLAlchemy query with OR/AND/like common filters

时间:2016-12-03 15:42:20      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:lis   nal   comm   size   sha   sed   out   query   sep   

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

标签:lis   nal   comm   size   sha   sed   out   query   sep   

原文地址:http://www.cnblogs.com/ymy124/p/6128602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!