One - One Code All

Blog Content

SQLAlchemy中模糊查询like多个关键字

Python   2016-05-30 16:40:06
from sqlalchemy import and_


words = ['%三%', '%猫%']
rule = and_(*[table.u_name.like(w) for w in words])
table.query.filter(rule)

DBSession().query(user).filter(user.u_name.like('%三%')).filter(user.u_name.like('%猫%'))
# 或者 and_()
DBSession().query(user).filter(and_(user.u_name.like('%三%'), user.u_name.like('%猫%')))



上一篇:flask-sqlalchemy中,将查询语句转换成原始SQL打印
下一篇:slf4j-api和slf4j-simple的关系,启动报错Failed to load class org.slf4j.impl.StaticLoggerBinder

The minute you think of giving up, think of the reason why you held on so long.