我正在寻找sql中一个相当复杂的查询的帮助。
查询应该听起来像:(首先查询下面) 我正在寻找类型的产品:床和我想显示分配给他们的所有选项 - 将它们分组并计算它们中有多少。 (如下图所示)。
查询的第二部分:(下面的第二个查询) 当用户单击复选框时,我希望仅显示和计算已检查选项的产品,并计算这些产品的数量。 (这是问题)
首先查询:
SELECT COUNT(DISTINCT id_product) as productMatched, COUNT(id_product) as haveProducts, product_to_features.id_feature_value, product_to_features.id_feature, feature_value_langs.name,feature_value_langs.id_feature_value, products.active
FROM product_to_features
LEFT JOIN feature_value_langs
ON product_to_features.id_feature_value = feature_value_langs.id_feature_value
LEFT JOIN products
ON product_to_features.id_product = products.id
WHERE price_retail >= 655.00
AND price_retail <= 6455.00
AND height >= 0.0 AND height <= 120.0
AND width >= 0.0 AND width <= 262.0
AND depth >= 0.0 AND depth <= 276.0
AND product_to_features.id_feature_value IN (15981,15982,15983,16011,16012,16013,16019) //its shows ids of bed sizes
AND products.active = 1
GROUP BY product_to_features.id_feature_value
第二个查询 - 不能正常工作:
SELECT COUNT(DISTINCT product_to_features.id_feature_value) as productMatched, COUNT(product_to_features.id_product) as haveProducts, product_to_features.id_feature_value, product_to_features.id_feature, id_product as ProductId, feature_value_langs.name, products.active
FROM product_to_features
LEFT JOIN feature_value_langs
ON product_to_features.id_feature_value = feature_value_langs.id_feature_value
LEFT JOIN products
ON product_to_features.id_product = products.id
WHERE price_retail >= 655 AND price_retail <= 6455
AND height >= 0 AND height <= 120
AND width >= 0 AND width <= 262
AND depth >= 0 AND depth <= 276
AND (product_to_features.id_feature_value IN (15981,15982,15983,16011,16012,16013,16019) // (its first checkbox group where non is checked - so i send all values )
OR product_to_features.id_feature_value IN ( 1096) // 1096 - its checked option from second checkbox group)
OR product_to_features.id_feature_value = 1483 ) // 1483 - is BED TYPE (must be)
AND products.active = 1
GROUP BY product_to_features.id_product having productMatched>2
ORDER BY product_to_features.id_feature
例如,如果我检查80x200,它必须刷新所有其他复选框并显示匹配当前选项(已选中)和计数匹配产品的数量。
这是我的数据库结构:
DESCRIBE product_to_features
id int(11) NO PRI auto_increment
id_feature int(10) unsigned NO MUL
id_product int(10) unsigned NO MUL
id_feature_value int(10) unsigned NO MUL
desc `feature_value_langs`
id int(11) NO PRI auto_increment
id_feature_value int(10) unsigned NO MUL
id_lang int(10) unsigned NO MUL
name varchar(255) YES MUL