Php 复杂sql查询-计数+;具有+;分组方式-如何修复查询
我正在寻找有关sql中一个相当复杂的查询的帮助 查询听起来应该像:(下面的第一个查询) 我正在寻找的产品类型是:床,我想显示分配给他们的所有选项-分组,并计算其中有多少。(如下图所示) 查询的第二部分:(下面的第二个查询) 当用户单击复选框时,我只想显示和统计已选中选项的产品,并统计这些产品的数量。(这就是问题所在) 第一个问题:Php 复杂sql查询-计数+;具有+;分组方式-如何修复查询,php,mysql,sql,Php,Mysql,Sql,我正在寻找有关sql中一个相当复杂的查询的帮助 查询听起来应该像:(下面的第一个查询) 我正在寻找的产品类型是:床,我想显示分配给他们的所有选项-分组,并计算其中有多少。(如下图所示) 查询的第二部分:(下面的第二个查询) 当用户单击复选框时,我只想显示和统计已选中选项的产品,并统计这些产品的数量。(这就是问题所在) 第一个问题: SELECT COUNT(DISTINCT id_product) as productMatched, COUNT(id_product) as haveProdu
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
请看您使用的是MySQL还是MS SQL Server?不要标记未涉及的产品。抱歉,我单击了错误的建议-当然是MySQL。现在也是了解别名和限定列名的重要性的好时机-例如,从my_table x中的
,其中x.price_retail>=655和x.price_retail=0和x.height
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
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