MySQL , " " โโ
, . , , .
{ "": "", "": "" }
{ " " }
{ "" : "", "" : "", "": "" }
{ "" : "", "" : "Medium", "": "" }
{ "" : "", "" : "", "": "" }
, opt_count 1.
To get around this, you need to get opt_count based only on color = red as a derived table, and then attach this back to the tables that have the rest of the data you are interested in.
I believe this query returns what you want. Note that you are not getting one row for each product, again because of the unique nature of the combination column.
SELECT
products.product_name, products.product_id,
product_variant_combinations.combination_id, product_variant_combinations.combination,
product_variant_attributes.attribute,
product_variant_options.option,
red_products.option_count
FROM product_variant_combinations
INNER JOIN product_variants
ON product_variant_combinations.combination_id = product_variants.combination_id
INNER JOIN product_variant_ao_relation
ON product_variants.ao_id = product_variant_ao_relation.ao_id
INNER JOIN product_variant_options
ON product_variant_ao_relation.option_id = product_variant_options.option_id
INNER JOIN product_variant_attributes
ON product_variant_ao_relation.attribute_id = product_variant_attributes.attribute_id
INNER JOIN products
ON product_variant_combinations.product_id = products.product_id
INNER JOIN
(
SELECT COUNT(DISTINCT product_variant_combinations.product_id) AS option_count,
product_variant_attributes.attribute_id,
product_variant_options.option_id
FROM product_variant_attributes
INNER JOIN product_variant_ao_relation
ON product_variant_attributes.attribute_id = product_variant_ao_relation.attribute_id
INNER JOIN product_variant_options
ON product_variant_ao_relation.option_id = product_variant_options.option_id
INNER JOIN product_variants
ON product_variant_ao_relation.ao_id
INNER JOIN product_variant_combinations
ON product_variants.COMBINATION_ID = product_variant_combinations.COMBINATION_ID
WHERE product_variant_options.option = 'red'
AND product_variant_attributes.attribute = 'color'
GROUP BY product_variant_attributes.attribute_id, product_variant_options.option_id
) AS red_products
ON product_variant_attributes.attribute_id = red_products.attribute_id
AND product_variant_options.option_id = red_products.option_id
source
share