我有一個表爲其他表的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