Say I have a table that has items and attributes listed like,
frog green
cat furry
frog nice
cat 4 legs
frog 4 legs
From the items column I want to select unique objects that have both the green and 4 legs attribute. I would expect to get back just the frog object in this case. What is the most efficient query to do this?
But maybe this can help you:
create two tables, one of items and one of attributes.
Items could be name, intAttributeID, where intAttributeID is a foreign key reference to the Attributes table. That way you can do a select statement based off whatever you care about.
You could also query each attribute separately, and then intersect them...
The most efficient way to do this is with a self-join:
Another solution that some people use is a trick with GROUP BY:
But the GROUP BY solution may not be as efficient as a JOIN, depending on which brand of RDBMS you use. Also one method may scale better as the volume in your table grows.
If possible, I would redesign. This is not something you will ever be able to effectively query 12 values on at the same time on (it will require 12 joins)
Please read this wikipedia article http://en.wikipedia.org/wiki/Entity-Attribute-Value_model#Downsides
Never seen a database yet that used this model that didn't run into serious performance issues eventually. This design looks elegant to non-database people but is actually usually a sign of a badly designed database.