2017-04-25 58 views
-3

不同列我有数据库的结构中示出的下面的图像中:选择从表

我想要检索variant_id属于特定product_id

实施例:

可以说1和Size = LColor = Green

我希望MySQL查询返回variant_id = 7

什么是在这种情况下所使用的查询期望?

Database Structure

+2

你已经试过了什么? –

回答

0

您可以使用下面的查询。 我假设你的表名是table1

select variant_id from (select * from table1 where 
product_id=1 and ((attribute_name='Size' and value='L') 
or (attribute_name='Color' and value='Green'))) as temp 
group by variant_id having count(*)>1 
+0

非常感谢你。它的作用像魅力。你让我今天一整天都感觉很好 –