mySQL:查询一对多表?

发布于 2024-10-05 02:30:49 字数 741 浏览 0 评论 0原文

在以下数据库设计中使用一对多方法查询具有特定属性的产品的适当方法是什么?

我想我应该做如下的事情: SELECT (*) FROM ProductProperties WHERE property = 'weight' AND value = '10'

但是如果我需要重量 = 10 和 value = '10' 的产品怎么办?在同一查询中 color = blue?

数据库设计示例:

表:产品

------------------------
id    | name     |  price
------------------------
0     | myName   |  100
1     | myName2  |  200

表:productProperties

------------------------------------------------
product  | property     |  Value
------------------------------------------------
0        | weight       |  10
1        | weight       |  20
1        | color        |  blue

What would be the appropriate way to query products with a specific property in the following database design with a one-to-many approach?

I guess that I should be doing something like the following:
SELECT (*) FROM productProperties WHERE property = 'weight' AND value = '10'

But what if I need to products that has both weight = 10 & color = blue in the same query?

Example of database design:

table: products

------------------------
id    | name     |  price
------------------------
0     | myName   |  100
1     | myName2  |  200

table: productProperties

------------------------------------------------
product  | property     |  Value
------------------------------------------------
0        | weight       |  10
1        | weight       |  20
1        | color        |  blue

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

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

发布评论

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

评论(2

神也荒唐 2024-10-12 02:30:49

如果我需要的产品有
两者的重量 = 10 &颜色 = 蓝色
相同的查询?

一个选项:

select product, name
  from products inner join productProperties
    on (products.id = productProperties.product)
 where (property = 'weight' and value = '10')
    or (property = 'color' and value = 'blue')
 group by product, name
having count(1) = 2

另一种带有子查询的选项:

select id, name
  from products p
 where exists (
         select 1
           from productProperties pp1
          where p.id = pp1.product 
            and pp1.property = 'weight'
            and value = '10'
       )
   and exists (
         select 1
           from productProperties pp2
          where p.id = pp2.product 
            and pp2.property = 'color'
            and value = 'blue'
       )

What if I need to products that has
both weight = 10 & color = blue in the
same query?

One option:

select product, name
  from products inner join productProperties
    on (products.id = productProperties.product)
 where (property = 'weight' and value = '10')
    or (property = 'color' and value = 'blue')
 group by product, name
having count(1) = 2

Another option with subqueries:

select id, name
  from products p
 where exists (
         select 1
           from productProperties pp1
          where p.id = pp1.product 
            and pp1.property = 'weight'
            and value = '10'
       )
   and exists (
         select 1
           from productProperties pp2
          where p.id = pp2.product 
            and pp2.property = 'color'
            and value = 'blue'
       )
会傲 2024-10-12 02:30:49
SELECT * FROM productProperties p 
WHERE (SELECT COUNT(*) FROM productProperties p1 WHERE p1.product = p.product AND 
( (property = 'weight' AND value = '10') OR (property = 'color' AND value = 'blue') ) 
 =2
SELECT * FROM productProperties p 
WHERE (SELECT COUNT(*) FROM productProperties p1 WHERE p1.product = p.product AND 
( (property = 'weight' AND value = '10') OR (property = 'color' AND value = 'blue') ) 
 =2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文