我需要在数据库(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