doucitan2544 2016-10-30 16:27
浏览 15
已采纳

如何获取自联接表的最后一个类别

I have a table like this (this table is my categories of products) :

id   title   parent_id 
 1     A         0
 2     A1        1
 3     A2        1
 4     B         0
 5     A11       2
 6     B1        4

now,How can I get last of sub-categories ? for example :

B1
A11
A2
  • 写回答

2条回答 默认 最新

  • doudi5524 2016-10-30 16:29
    关注

    You seem to want the rows that are not the parent of another row:

    select pc.*
    from productcategories pc
    where not exists (select 1
                      from productcategories pc2
                      where pc2.parent_id = pc.id
                     );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?