'Select foreign key matching multiple criteria in a joining table (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.
Item | Type | Value |
---|---|---|
Item #100 | Color | Red |
Item #100 | Type | Hat |
Item #101 | Color | Blue |
Item #101 | Size | S |
Item #101 | Type | T-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 Intersect
ing 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...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|