SQLAlchemy 等价于 SQL“LIKE";声明 [英] SQLAlchemy equivalent to SQL "LIKE" statement
本文介绍了SQLAlchemy 等价于 SQL“LIKE";声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标签列具有苹果香蕉橙"和草莓香蕉柠檬"之类的值.我想找到 SQLAlchemy 等效语句
A tags column has values like "apple banana orange" and "strawberry banana lemon". I want to find the SQLAlchemy equivalent statement to
SELECT * FROM table WHERE tags LIKE "%banana%";
我应该向 Class.query.filter()
传递什么来执行此操作?
What should I pass to Class.query.filter()
to do this?
推荐答案
每列都有like()
方法,可以在query.filter()
中使用.给定一个搜索字符串,在任一侧添加一个 %
字符以作为两个方向的子字符串进行搜索.
Each column has like()
method, which can be used in query.filter()
. Given a search string, add a %
character on either side to search as a substring in both directions.
tag = request.form["tag"]
search = "%{}%".format(tag)
posts = Post.query.filter(Post.tags.like(search)).all()
这篇关于SQLAlchemy 等价于 SQL“LIKE";声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文