作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
两个表
1) 产品
--------------------
id | Name | price
1 | p1 | 10
2 | p2 | 20
3 | p3 | 30
2) product_attributes
:
---------------------------------------------------
id | product_id | attribute_name | attribute_value
---------------------------------------------------
1 | 1 | size | 10
2 | 1 | colour | red
3 | 2 | size | 20
我需要连接这两个表。在 where 子句中,我需要同时匹配两行属性值。是否可以根据两行得到结果值(value)。根据用户选择,我需要展示所有符合条件的产品。这里如果 size=10 和 colour=red。
输出应该是
1 | p1 | 10
获得对此的查询可能会非常有帮助。
最佳答案
select p.* from product p
join (select a1.product_id id from product_attributes a1 join product_attributes a2 using (product_id)
where a1.attribute_name = 'size' and a1.attribute_value = '10'
and a2.attribute_name = 'colour' and a2.attribute_value = 'red') pa
using (id)
如果需要匹配更多的属性,只需要在子查询中加入更多的join即可。
关于mysql - 加入从多个行值中选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666365/
我是一名优秀的程序员,十分优秀!