MySQL,SQL查询多行

时间:2016-08-31 10:37:52

标签: php mysql key-value key-value-store

我有一个名为clothes_properties的表,其中一些行如下所示:

id  product_id  property_name   property
55  189000      color           blue
56  189000      type            tshirt
57  189000      size            medium
58  189001      color           red
59  189001      type            tshirt
60  189001      size            medium

我想创建一个SQL查询,它能够从用户搜索的内容中选择正确的product_id。因此,如果用户搜索tshirt和medium,我希望SQL查询返回:189000& 189001.如果用户搜索tshirt和蓝色,我只想要返回此产品ID:189000。如何创建执行此操作的SQL查询?

4 个答案:

答案 0 :(得分:1)

这种表通常称为键/值存储。它是处理可扩展属性列表的有效方法,但使用起来可能有点麻烦。

这样的查询会按照与您的条件列表匹配的顺序为您提供product_id值,最先匹配。 (http://sqlfiddle.com/#!9/7870b5/2/0

select count(*) matches, product_id
  from prop
 where property in ('tshirt','medium')
 group by product_id
 order by 1 desc 

但是,此查询不知道大小,颜色和类型之间的区别。

如果你想精确匹配尺寸,颜色等,它会变得更加复杂。您需要从一个支持键/值属性表的查询开始 - 将行转换为列。 (http://sqlfiddle.com/#!9/7870b5/3/0

select id.product_id, 
       color.property color,
       type.property type,
       size.property size
 from (select distinct product_id from prop) id
 left join prop color on id.product_id = color.product_id and color.property_name = 'color'
 left join prop type on id.product_id = type.product_id and type.property_name = 'type'
 left join prop size on id.product_id = size.product_id and size.property_name = 'size'

然后,您需要将其视为虚拟表并对其进行查询,可能是这样。 (http://sqlfiddle.com/#!9/7870b5/4/0

select *
  from (

        select id.product_id, 
               color.property color,
               type.property type,
               size.property size
         from (select distinct product_id from prop) id
         left join prop color on id.product_id = color.product_id and color.property_name = 'color'
         left join prop type on id.product_id = type.product_id and type.property_name = 'type'
         left join prop size on id.product_id = size.product_id and size.property_name = 'size'
         ) allprops
 where size='medium' and color = 'blue'

许多开发人员和dbas将创建一个类似allprops的视图,以使其更容易。

答案 1 :(得分:0)

您是否要完全忽略property_name列?

SELECT DISTINCT product_id
FROM   clothes_properties
WHERE  property IN ('tshirt', 'medium');

SELECT DISTINCT product_id
FROM   clothes_properties
WHERE  property IN ('tshirt', 'blue');

答案 2 :(得分:0)

我会使用GROUP BYHAVING

执行此操作
select product_id
from clothes_properties
where property_type = 'type' and property = 'tshirt' or
      property_type = 'size' and property = 'medium'
group by product_id
having count(distinct property_type) = 2;

注意:如果您没有为赠品提供重复的属性类型,则不需要distinct

如果您想要通用的东西,可以使用:

select product_id
from clothes_properties
where property_type = 'type' and (property = $type or $type = '') or
      property_type = 'size' and (property = $size or $size = '') or
      property_type = 'color' and (property = $color or $color = '')
group by product_id
having count(distinct property_type) = (($type <> '') + ($size <> '') + ($color <> ''));

答案 3 :(得分:0)

  

试试这个

SELECT DISTINCT(product_id) FROM clothes_properties WHERE property IN ('tshirt','medium') group by product_id having count (distinct property) = 2