sqlalchemy检查列是否包含数组中的任何子字符串
我有一个称为player
的SQLalchemy表,该表具有位置
以及其他类似的篮球玩家列。 位置中的任何值
将是guard
,forward
,或中心
。
给定用户希望看到的一系列职位] ,我想查询位置
列以查看是否在数组中的任何位置匹配符合用户过滤的其他条件的行, 作为年龄,体重,身高等。
例如, 30&位置= [Guard,Forward] ,预期的结果将是所有40岁以下的球员,均为后卫或卵形。
我熟悉.contains()
列方法。我试图运行类似于query.filter(player.position.contains(positions)))
,但是我遇到问题,因为positions
是一个数组,而不是字符串。
由于只有3个可能的位置,因此一个真正的骇人听闻的替代方法是执行一些涵盖每个场景的陈述(数组中的1个位置,所有3个,一定的组合等),但是这将很难维护如果我有更多职位。
有什么想法吗?
I have a SQLAlchemy table called Player
which has a column Position
, along with other similar columns for basketball players. Any value within Position
will either be Guard
, Forward
, or Center
.
Given an array of positions that the user would like to see results for, such as [Guard, Forward]
or [Guard]
or [Forward, Center, Guard]
, I would like to query the Position
column to see if any positions within the array match the row that meets the other criteria the user filtered for, such as age, weight, height, etc.
For example, if the user searched for a Player with filters looking like age < 30 & position = [Guard,Forward]
, the expected result would be all Players who are under 40 and are guards or fowards.
I am familiar with the .contains()
method for columns. I was attempting to run something similar to query.filter(Player.position.contains(positions))
but I was running into issues because positions
is an array and not a string.
Since there are only 3 possible positions, one really hacky alternative is to do a few if statements that cover every single scenario (1 position in the array, all 3, a certain combo, etc...) but this will be hard to maintain if I had more positions.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论