我需要在數據庫(mySQL)中以鍵值對的形式存儲少量項目及其屬性。我打算如下進行。需要一個MySQL查詢從存儲鍵值對的表中進行選擇
我將使用兩個表items
和item_properties
。
items
itemId | itemName ------------------- 1923 | AC 1235 | Fridge 8273 | Heater
item_properties
itemId | property | value -------------------------------- 1923 | effect | cooling 1923 | consumption | efficient 1923 | type | split 1235 | effect | cooling 1235 | volume | 20 liters 8273 | effect | heating 8273 | consumption | efficient 8273 | heatMethod | coil
現在,如果我有選擇項,它們的 '效果' 是 '冷卻',我能做到這一點使用下面的查詢(這會給我' AC'和'Fridge')。
SELECT itemName FROM items i, item_properties p WHERE i.itemId=p.itemId AND (p.property = 'effect' AND p.value ='cooling');
我想知道如何編寫查詢來選擇匹配多個屬性,如
- 的項目選擇其「效果」是「冷」與「消費」的所有項目是「有效的」(這會匹配項目'AC')。
- 選擇'type'爲'split'或'heatMethod'爲'coil'或OR'consumption'爲'effecient'(可匹配項目「AC」和「Heater」)的所有項目。
請幫助...提前致謝!
謝謝@WoLpH! :)爽脆簡單... – Goje87 2010-11-27 10:35:29