首页 > 代码库 > SQLAlchemy query with OR/AND/like common filters
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 ANDfilter(User.name == ‘leela‘, User.fullname == ‘leela dharan‘)# or call filter()/filter_by() multiple timesfilter(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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。