Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL过滤复杂查询_Mysql_Sql - Fatal编程技术网

Mysql SQL过滤复杂查询

Mysql SQL过滤复杂查询,mysql,sql,Mysql,Sql,我有三张桌子: 产品 PRODUCT_ID|NAME 1 |Elaborado 2 |Adidas 产品属性 PRODUCT_ATTRIBUTE_ID|PRODUCT_ID|ATTRIBUTE_ID|VALUE 1 |1 |1 |Solid 2 |1 |2 |Casual 3

我有三张桌子:

产品

PRODUCT_ID|NAME
     1    |Elaborado  
     2    |Adidas
产品属性

 PRODUCT_ATTRIBUTE_ID|PRODUCT_ID|ATTRIBUTE_ID|VALUE
         1           |1         |1           |Solid
         2           |1         |2           |Casual
         3           |2         |1           |Solid
ATTRIBUTE_ID|DESCRIPTION
         1  |Pattern
         2  |Occasion
属性

 PRODUCT_ATTRIBUTE_ID|PRODUCT_ID|ATTRIBUTE_ID|VALUE
         1           |1         |1           |Solid
         2           |1         |2           |Casual
         3           |2         |1           |Solid
ATTRIBUTE_ID|DESCRIPTION
         1  |Pattern
         2  |Occasion
所有属性都在属性表中。产品和属性之间的链接位于产品属性中。 现在,我需要根据属性筛选产品:

例如: 我需要图案“坚固”且场合“休闲”的产品:

结果应如下所示:

PRODUCT_ID
----------
1
我正在使用Mysql数据库。 我准备了一把小提琴:

请帮忙

谢谢, 窗扇。

看看这个

select group_concat(product.name),attribute.DESCRIPTION,product_attribute.value  from product
join product_attribute
on (product_attribute.product_id=product.product_id)
join attribute
on (product_attribute.attribute_id=attribute.attribute_id)
where (attribute.attribute_id = 1 and product_attribute.value = 'Solid')
or 
(attribute.attribute_id = 2 and product_attribute.value = 'Casual')
group by attribute.attribute_id, product_attribute.value

将列存储为行的概念被调用,这使得查询变得复杂:)例如,您可以找到“pattern=solid和evency=casual”这样的语句:

另一种方法是创建旋转视图,如:

create view vw_product_attribute as
select  p.product_id
,       pa_pattern.value as pattern
,       pa_occasion.value as occasion
from    product p
join    product_attribute pa_pattern
on      p.product_id = pa_pattern.product_id
join    attribute a_pattern
on      pa_pattern.attribute_id = a_pattern.attribute_id
join    product_attribute pa_occasion
on      p.product_id = pa_occasion.product_id
join    attribute a_occasion
on      pa_occasion.attribute_id = a_occasion.attribute_id
where   a_pattern.description = 'Pattern'
        and a_occasion.description = 'Occasion'
然后,您可以进行如下查询:

select  product_id
from    vw_product_attribute
where   pattern = 'Solid'
        and occasion = 'Casual'

这对你有用吗?您需要在设置patternSolidProduct变量和SELECT语句的子查询上设置描述和值

您还应该在
属性
属性(id)
产品
产品(id)
产品(属性)
产品(id)

我将索引添加到
product\u属性
中,长度为10,
属性
描述中,长度为10

/*Start join method */
/* Get product id's matching pattern = solid */
SET @patternSolidProduct := (SELECT
  GROUP_CONCAT(`pa`.`product_id`)
FROM
  `product_attribute` AS `pa`
LEFT JOIN
  `product` as `p`
ON
  `pa`.`product_id` = `p`.`product_id`
LEFT JOIN
  `attribute` AS `a`
ON
  `a`.`attribute_id` = `pa`.`attribute_id`
WHERE
  `a`.`description` = 'Pattern'
    AND `pa`.`value` = 'Solid'
);
/* End product id's matching pattern = solid */

SELECT
  `pa`.`product_id`
FROM
  `product_attribute` AS `pa`
LEFT JOIN
  `product` as `p`
ON
  `pa`.`product_id` = `p`.`product_id`
LEFT JOIN
  `attribute` AS `a`
ON
  `a`.`attribute_id` = `pa`.`attribute_id`
WHERE
  `a`.`description` = 'Occasion'
  AND `pa`.`value` = 'Casual'
  AND `p`.`product_id` IN (@patternSolidProduct)
/*End join method */
从上面可以看出:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'p', 'const', 'PRIMARY', 'PRIMARY', '8', 'const', '1', 'Using index'
'1', 'SIMPLE', 'pa', 'ref', 'value', 'value', '32', 'const', '1', 'Using where'
'1', 'SIMPLE', 'a', 'eq_ref', 'PRIMARY,description', 'PRIMARY', '8', 'sqlfiddle.pa.attribute_id', '1', 'Using where'

我会制作一个伪标准化的轴心,并对其进行过滤。这不是最快的方法,但在考虑各种过滤要求的混合时,这是最简单的方法。谢谢,但是如果要过滤的属性数量是动态的呢?@sash:您可以将所有已知属性添加到视图中。对于数量可变的属性,您必须构建自定义SQL语句谢谢。你是说我需要使用代码动态生成尽可能多的连接?我可以为任意数量的属性编写一个查询吗?可能吗?谢谢