我在MySQL中有以下表结构和数据
TABLE NAME : categories CatID CatName ParentCatID ------------------------------- 1 Shirts NULL 2 Short Sleev 1 3 Long Sleev 1 4 Collarless 2 5 Collar 2 6 Collarless 3 7 Collar 3 8 Square Cut 4 9 Round Cut 4 10 Square Cut 6 11 Round Cut 6 table name : companies ------------------------------------------------- companyid | company name | country | categoryid ------------------------------------------------- 1 | Unitile ltd. | India | 5 2 | abc ltd. | India | 2 3 | infocom ltd. | India | 1 4 | tata ltd. | India | 5 5 | agro india | India | 1
我有2个类别的类别:衬衫> Long Sleev
我希望在衬衫
之类的父类别下记录总记录数答案 0 :(得分:0)
我不明白公司表与这个问题有什么关系。这样的事情可能会做你想要实现的目标
SELECT
C.CatID, COUNT(DISTINCT (IFNULL(PC.CatID, 0))) AS parentcategories
FROM categories C
LEFT JOIN categories PC ON (C.ParentCatID = PC.CatID)
GROUP BY C.CatID
;