sqlalchemy检查列是否包含数组中的任何子字符串

发布于 2025-02-07 21:22:17 字数 602 浏览 1 评论 0原文

我有一个称为player的SQLalchemy表,该表具有位置以及其他类似的篮球玩家列。 位置中的任何值将是guardforward,或中心

给定用户希望看到的一系列职位] ,我想查询位置列以查看是否在数组中的任何位置匹配符合用户过滤的其他条件的行, 作为年龄,体重,身高等。

例如, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文