在联合表(SQLite)中选择匹配多个标准的外键匹配多个标准

发布于 2025-01-28 21:11:43 字数 1255 浏览 0 评论 0原文

序言:这一切都在小提琴中 /f/pmlfrz59ael7b2jusmtt9x/1


我有一个包含项目的表(item#100,item#101 ...),每个表都有各种属性(code> colod> color,type,type,stype,size,size size)。因为一组属性不一致(例如,在下表中,item#100是帽子,通常不具有尺寸),所以我创建了一张属性类型的项目地图和每个项目的属性值。

项目类型
项目#100颜色红色
项目#100类型帽子
项目#101颜色蓝色
项目#101大小S
项目#101类型T恤

SNIP

但是我现在正在努力通过任意集来检索项目在多个标准中 - 例如,我不确定如何选择具有:

属性类型size带有属性值s s

的所有项目属性类型颜色,属性值blue

(示例,查询将返回iteg <代码>项目#101item#103 < /code>)

我通过选择所有单个标准成功地检索了行,然后Intersect一起(例如,选择“ S”的所有内容与选择“ blue”的所有内容相交)。这似乎不是一个很好的解决方案,因为要查询会更改的完整标准(有时只能通过一个属性,有时会通过多个属性),

我在所有这些方面都相对较新,但是通过SQLite的文档阅读关于正确社区中的索引...

Preface: this is all in a fiddle here https://www.db-fiddle.com/f/pMLFrz59aeL7B2jUsmTt9X/1


I have a table that contains items (item #100, item #101...) that each have various attributes (color, type, size). Because the set of attributes are not consistent (e.g. in the table below, item #100 is a hat, which don't typically have a size), I created a map of the items to the attribute type and attribute value for each of the items.

ItemTypeValue
Item #100ColorRed
Item #100TypeHat
Item #101ColorBlue
Item #101SizeS
Item #101TypeT-Shirt

Snip

However I am now struggling to retrieve items by an arbitrary set of multiple criteria — for example, I am unsure of how to select all items that have:

Attribute type Size with attribute value s

AND

Attribute type Color, attribute value Blue

(which, for the example, the query would return Item #101 and Item #103)

I successfully retrieved the rows with selecting all of the individual criteria and then Intersecting them together (e.g. select everything of size "s" intersected with select everything of color "blue"). That didn't seem like a good solution as the full set of criteria to query by will change (sometimes it will only by one attribute, sometimes it will be by multiple)

I am relatively new at all of this, but reading through sqlite's documentation about indices felt in the right neighborhood...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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