Sql类目商品数量查询
Sql categories products count query
你好,我有一个问题,如果有人能帮忙就太好了
我想做的是按所选类别获取所有计数。例如,如果我没有选择任何类别,结果将是这样的:
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 2 ) <- Product-57, Product-50
Category-61 ( 1 ) <- Product-56
Category-73 ( 1 ) <- Product-50
这很简单。我有一个这样的查询:
http://sqlfiddle.com/#!9/4f188/1
所以我想将类别 ID 传递给我的查询,并根据此 ID 获取计数,如果我传递类别 ID 70,则结果必须是
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 1 ) <- Product-57, [Product-50 is gone because is not in cateogry id 70]
Category-61 ( 0 )
Category-73 ( 0 )
如果我传递类别 ID 70 和 64,结果必须是
Category-70 ( 1 ) <- Product-57, [Product-56 is gone because is not in category 70 and 64]
Category-64 ( 1 ) <- Product-57
Category-61 ( 0 ) [Product-56 is gone, because is not in category 70 and 64 ]
Category-73 ( 0 ) [Product-50 is gone because is not in category 70 and 64]
或者如果我给出参数类别 id 73,结果必须是
Category-70 ( 0 ) [products are not counted because they are not in 73]
Category-64 ( 1 ) <- Product-50
Category-61 ( 0 ) [products are not counted because they are not in 73]
Category-73 ( 1 ) <- Product-50
这有可能吗:),请帮忙...
+1 用于 SQL fiddle 示例,非常有用。
我相信这应该可以解决您的问题(在您的加入中添加了子 select)
SELECT Concat('Category-',c.category_id),
count(DISTINCT p2c.product_id) as products
FROM category c
LEFT JOIN product_to_category p2c
ON (c.category_id = p2c.category_id AND p2c.product_id) AND p2c.product_id in
(select product_id from product_to_category where category_id = @input)
LEFT JOIN category_path cp ON (cp.category_id = c.category_id AND cp.path_id = c.category_id)
WHERE
cp.level <= 2
GROUP BY c.category_id
ORDER BY c.sort_order
你好,我有一个问题,如果有人能帮忙就太好了
我想做的是按所选类别获取所有计数。例如,如果我没有选择任何类别,结果将是这样的:
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 2 ) <- Product-57, Product-50
Category-61 ( 1 ) <- Product-56
Category-73 ( 1 ) <- Product-50
这很简单。我有一个这样的查询:
http://sqlfiddle.com/#!9/4f188/1
所以我想将类别 ID 传递给我的查询,并根据此 ID 获取计数,如果我传递类别 ID 70,则结果必须是
Category-70 ( 2 ) <- Product-57, Product-56
Category-64 ( 1 ) <- Product-57, [Product-50 is gone because is not in cateogry id 70]
Category-61 ( 0 )
Category-73 ( 0 )
如果我传递类别 ID 70 和 64,结果必须是
Category-70 ( 1 ) <- Product-57, [Product-56 is gone because is not in category 70 and 64]
Category-64 ( 1 ) <- Product-57
Category-61 ( 0 ) [Product-56 is gone, because is not in category 70 and 64 ]
Category-73 ( 0 ) [Product-50 is gone because is not in category 70 and 64]
或者如果我给出参数类别 id 73,结果必须是
Category-70 ( 0 ) [products are not counted because they are not in 73]
Category-64 ( 1 ) <- Product-50
Category-61 ( 0 ) [products are not counted because they are not in 73]
Category-73 ( 1 ) <- Product-50
这有可能吗:),请帮忙...
+1 用于 SQL fiddle 示例,非常有用。
我相信这应该可以解决您的问题(在您的加入中添加了子 select)
SELECT Concat('Category-',c.category_id),
count(DISTINCT p2c.product_id) as products
FROM category c
LEFT JOIN product_to_category p2c
ON (c.category_id = p2c.category_id AND p2c.product_id) AND p2c.product_id in
(select product_id from product_to_category where category_id = @input)
LEFT JOIN category_path cp ON (cp.category_id = c.category_id AND cp.path_id = c.category_id)
WHERE
cp.level <= 2
GROUP BY c.category_id
ORDER BY c.sort_order