title: Complex sql query - count + having + group by - how fix query
body: <p>i'm looking for help with one quite complex query in sql. </p>
  
  <p>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). </p>
  
  <p>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 )</p>
  
  <p>First query:</p>
  
  <pre><code>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 &gt;= 655.00 
     AND price_retail &lt;= 6455.00 
     AND height &gt;= 0.0 AND height &lt;= 120.0 
     AND width &gt;= 0.0 AND width &lt;= 262.0 
     AND depth &gt;= 0.0 AND depth &lt;= 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
  </code></pre>
  
  <p>And this shows:
  <a href="https://i.stack.imgur.com/KBw7d.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KBw7d.jpg" alt="And this shows:"></a></p>
  
  <p>Second query - not working as it should be:</p>
  
  <pre><code>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 &gt;= 655 AND price_retail &lt;= 6455 
     AND height &gt;= 0 AND height &lt;= 120 
     AND width &gt;= 0 AND width &lt;= 262 
     AND depth &gt;= 0 AND depth &lt;= 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&gt;2 
  ORDER BY product_to_features.id_feature
  </code></pre>
  
  <p>For example if i check 80x200 it must refresh  all other checkboxes and show how many match current options (checked) and count matching products. </p>
  
  <p>And here is my db structure:</p>
  
  <pre><code>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         
  </code></pre>