i'm looking for help with one quite complex query in sql.
Query should sounds like: (First query below) I'm looking for products whitch are type: Beds and i would like to show all options assigned to them - group them and count how many of them are. (like in photo below).
Second part of query: (second query below) When user click checkbox i would like to show and count only products that have checked options and count how many of this products are. (and here is problem )
First query:
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
Second query - not working as it should be:
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
For example if i check 80x200 it must refresh all other checkboxes and show how many match current options (checked) and count matching products.
And here is my db structure:
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