我有一个表为其他表的entrys保存元数据。元数据表看起来像这样(我删除与ID,以便它更明显):MySQL:多次搜索单个表?
id entry_id property value
1 12 color blue
2 12 shape circle
3 13 color red
4 13 shape circle
5 14 color blue
6 14 shape square
7 15 material stone
8 12 material plastic
现在我要搜索此表的属性,如选择所有条目,其中颜色是蓝色 :
select entry_id from table where property = 'color' and value = 'blue'
到目前为止,一切都很好。但是,如何在有多个条件时扩展查询?例如,我想搜索所有条目,其中颜色是蓝色和形状是圈子。现在我想与工会实现这一目标:
select entry_id from table where property = 'color' and value = 'blue'
union
select entry_id from table where property = 'shape' and value = 'circle'
这显然变得丑陋了更多的属性我想看看。我认为这也不是很快。有没有更优雅的方式来做到这一点?这张表的原因是,我有对象与元数据可以由用户设置。
谢谢!
要获得godd的性能,不管你采取什么解决方案,你必须创建一个索引(entry_id,property,value)。如果这个三元组不是主键。 – 2009-08-14 13:53:44