需要一个 MySQL 查询来从存储键值对的表中进行选择

发布于 2024-10-04 05:32:21 字数 1011 浏览 0 评论 0原文

我需要在数据库(mySQL)中以键值对的形式存储一些项目及其属性。我计划按如下方式进行。

我将使用两个表 itemsitem_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”和“结果是冰箱)。

SELECT itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId 
AND (p.property = 'effect' AND p.value ='cooling');

我想知道如何编写查询来选择与多个属性匹配的项目,例如

  • 选择“效果”为“冷却”且“消耗”为“高效”的所有项目(将与项目“AC”匹配)。
  • 选择“类型”为“分体”或“heatMethod”为“线圈”或“消耗”为“高效”的所有项目(将匹配项目“AC”和“加热器”)。

请帮助...提前致谢!

I need to store few items and its properties in form of a key value pairs in the database (mySQL). I am planning to do it as following.

I'll use two tables items and 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

Now, if I have to select items whose 'effect' is 'cooling', I can do that using following query (which will give me 'AC' and 'Fridge' in result).

SELECT itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId 
AND (p.property = 'effect' AND p.value ='cooling');

I would like to know how write queries to select items that match multiple properties like

  • select all items whose 'effect' is 'cooling' AND 'consumption' is 'efficient' (which would match item 'AC').
  • select all items whose 'type' is 'split' OR 'heatMethod' is 'coil' OR 'consumption' is 'effecient' (which would match items 'AC' and 'Heater').

Kindly Help... Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-10-11 05:32:21

下面是一个示例查询:

SELECT
  itemName
FROM
  items i,
JOIN
  item_properties effect
  ON i.itemId = effect.itemId AND effect.property = 'effect'
JOIN
  item_properties consumption
  ON i.itemId = consumption.itemId AND consumption.property = 'consumption'

WHERE effect.value = 'cooling' AND consumption.value = 'efficient';

我将保留 oR 查询作为您可以自己尝试的内容。它只是添加更多表并在 WHERE 中使用 OR 而不是 AND

Here is an example query:

SELECT
  itemName
FROM
  items i,
JOIN
  item_properties effect
  ON i.itemId = effect.itemId AND effect.property = 'effect'
JOIN
  item_properties consumption
  ON i.itemId = consumption.itemId AND consumption.property = 'consumption'

WHERE effect.value = 'cooling' AND consumption.value = 'efficient';

I'll leave the oR query as something you can try yourself. It's simply adding more tables and using OR instead of AND in the WHERE.

弃爱 2024-10-11 05:32:21

问候!

我认为你原来的查询可能不正确..如果你的 item_properties 表有名为 (itemId, property, value) 的列,那么你的查询应该是:

SELECT i.itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND p.property='cooling';

另外,你在这里做“隐式连接”,我不知道如何你想了解更多关于 SQL 的知识,而不是只想让一些东西发挥作用,但是还有另一种方法来编写你的查询,我认为如果你打算暂时使用 SQL 数据库,这种方法可能会更好。这没什么大不了的,只是第二种形式对我来说更容易阅读。

您的原始查询:

SELECT itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND p.property='cooling';

将使用连接语法重写为:

SELECT i.itemName FROM items i 
JOIN item_properties p ON i.itemId=p.itemId WHERE p.property='cooling';

我将尝试给出两种形式来回答您的问题...

选择“效果”为“冷却”且“消耗”为“高效”的所有项目(这将匹配项“AC”)。

select itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND (p.property='cooling' AND p.value='consumption');

select i.itemName FROM items i JOIN item_properties p ON i.itemId=p.itemId 
WHERE p.property='cooling' AND p.value='consumption';

选择“类型”为“分体”或“heatMethod”为“线圈”或“消耗”为“高效”的所有项目(将匹配项目“AC”和“加热器”)。

select itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND ((p.property='type' AND p.value='split') OR 
(p.property='heatMethod' AND p.value='coil') OR 
(p.property='consumption' AND p.value='efficient'));

select itemName FROM items i JOIN item_properties p ON i.itemId=p.itemId 
WHERE (p.property='type' AND p.value='split') OR 
(p.property='heatMethod' AND p.value='coil') OR 
(p.property='consumption' AND p.value='efficient');

希望有帮助!

Greetings!

I think your original query is maybe not right.. if your item_properties table has columns named (itemId, property, value) then your query should be:

SELECT i.itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND p.property='cooling';

Also, you're doing "implicit joins" here, and I don't know how much you want to learn about SQL vs. just wanting to get something to work, but there is another way to write your queries that I think is perhaps better if you're planning to stick with SQL databases for a while. It's no big deal, just that the second form is easier to read for me.

Your original query:

SELECT itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND p.property='cooling';

Would be rewritten using join syntax as:

SELECT i.itemName FROM items i 
JOIN item_properties p ON i.itemId=p.itemId WHERE p.property='cooling';

I'll try to give both forms in answer to your questions...

select all items whose 'effect' is 'cooling' AND 'consumption' is 'efficient' (which would match item 'AC').

select itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND (p.property='cooling' AND p.value='consumption');

select i.itemName FROM items i JOIN item_properties p ON i.itemId=p.itemId 
WHERE p.property='cooling' AND p.value='consumption';

select all items whose 'type' is 'split' OR 'heatMethod' is 'coil' OR 'consumption' is 'effecient' (which would match items 'AC' and 'Heater').

select itemName FROM items i, item_properties p 
WHERE i.itemId=p.itemId AND ((p.property='type' AND p.value='split') OR 
(p.property='heatMethod' AND p.value='coil') OR 
(p.property='consumption' AND p.value='efficient'));

select itemName FROM items i JOIN item_properties p ON i.itemId=p.itemId 
WHERE (p.property='type' AND p.value='split') OR 
(p.property='heatMethod' AND p.value='coil') OR 
(p.property='consumption' AND p.value='efficient');

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文