0
按照article的說明,我想獲得一個完整的樹,每個類別的深度都能正常工作。在一個查詢中嵌套集合,產品數量和類別深度
我也想獲得每個類別的產品數量。我嘗試添加另一個COUNT(products_categories.product_id),但它與深度值混淆了。如何修改查詢以獲得每個類別的產品數量和深度?
products_categories
----------------------
category_id
product_id
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft
換句話說,我想結合這兩個查詢。
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft
SELECT parent.name, COUNT(product.name)
FROM nested_category AS node ,
nested_category AS parent,
product
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.category_id = product.category_id
GROUP BY parent.name
ORDER BY node.lft;
你的問題需要改進,我還是不完全明白你需要什麼。 –
我編輯了我的問題。請檢查。 – Khronos