mySQL:查询一对多表?
在以下数据库设计中使用一对多方法查询具有特定属性的产品的适当方法是什么?
我想我应该做如下的事情: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个选项:
另一种带有子查询的选项:
One option:
Another option with subqueries: