【问题标题】:Select distinct column from table从表中选择不同的列
【发布时间】:2017-09-21 21:21:46
【问题描述】:

我的数据库结构如下图所示:

我想检索属于特定product_idvariant_id

例子:

让我们说 1 和 Size = LColor = Green

我希望 mysql 查询返回 variant_id = 7。

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

【问题讨论】:

    标签: php mysql database laravel


    【解决方案1】:

    您可以使用以下查询。 我假设你的表名是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
    

    【讨论】:

    • 非常感谢。它就像魅力一样。你成就了我的一天
    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多