うーん、これは、これを実現するための2つの論理的な手順が欠落している可能性があります。
必要なのは、AttributeTypesテーブルとAttributeValuesテーブルです。
objects
id (integer)
name (string)
attribute_types
id (integer)
name (string)
description (string)
enabled (bit)
attribute_values
id (integer)
attribute_type_id (integer)
value (string)
enabled (bit)
attributes
id (integer)
object_id (integer)
attribute_type_id (integer)
attribute_value_id (integer)
したがって、オブジェクトは属性を持つことができます。これらの属性は、attribute_type_idおよびattribute_value_idによって指定されます。
これにより、複数の属性を持つオブジェクトを作成できます。
例えば
object
-> 1, ball
attribute_types
-> 10, Colour, null, enabled
-> 20, Shape, null, enabled
-> 30, Material, null, enabled
attribute_values
-> 100, 10, blue, enabled
-> 200, 10, red, enabled
-> 300, 10, green, enabled
-> 400, 20, round, enabled
-> 500, 20, square, enabled
-> 600, 20, triangle, enabled
したがって、属性は次のようになります。
attributes
-> 1000, 1, 10, 100 // this item has a color and it is blue
-> 1001, 1, 20, 400 // this item has a shape and it is round
-> 1002, 1, 10, 200 // this item has a color and it is red
そのため、オブジェクトは、異なるテーブルで値を指定する複数の属性を持つことができます。ここで重要な質問は、これをどのように照会しますか?SQLの強度によっては、クエリを複数の部分に分割する必要がある場合があります。
@attribute_type_id = select Id from attribute_types where name = 'Color' // 10
select * from objects
inner join attributes on objects.id = attributes.object_id
inner join attribute_values on objects.attribute_value_id = attribute_values.id
where attribute_type_id = @attribute_type_id
and attribute_values.value= 'blue'
そして、そこに、Colorのattribute_typeとblueのattribute_valueを持つすべてのオブジェクトを戻す必要があります。
My Sqlはそれほど強力ではありませんが、一度に複数の属性を検索する場合は、複数の句を実行できるはずです。私の考えでは、属性テーブルのattribute_type_idはattribute_typeテーブルに結合されていませんが、1ヒットでクエリを実行しやすくすることができます。パフォーマンスの面では、結合する必要がないため、クエリが高速化されます。テーブルですが、すべてがどれだけ大きくなるかによっては、違いは無視できるかもしれません。
注:私は通常、mysqlではなくmsssqlを使用しているため、データベースの種類が一致しない場合はそれが理由です。