douhu4692 2011-02-27 11:17 采纳率: 100%
浏览 277
已采纳

类别和子类别

I've asked a question few hours ago - afterwards decided to change my sql structure.

I have 2 tables:

categories (id,title,parent_id)
articles (id,title,content,parent_id)

each article can have only 1 category,
each category can have 1 parent category


an example:

article (id = 1) has : parent_id = 3
category (id = 3,'root') has: parent_id = 2
category (id = 2,'sub') has: parent_id = 1
category (id = 1,'subsub') has: parent_id = 0

I want to generate breadcrumbs for - article (id=1)
That would be shown as: Root->sub->subsub

What is the efficient way to accomplish this?

Thanks!

  • 写回答

2条回答 默认 最新

  • dongzi5062 2011-02-27 11:21
    关注
    1. When loading article, read all categories recursively and display breadcrumbs.

    2. If you need get all parents more than one time per script (e.g. when you are loading 50 articles and need parents for each), use the nested set model, as explained in Managing Hierarchical Data in MySQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了