duanlaofu4108 2009-10-20 18:49
浏览 27
已采纳

在一个SQL查询中向后调度父项?

I have a MySQL database (created by Wordpress) and this is similar to what it looks like:

ID    parentID     otherStuff
54    55           this is a test
55    56           another test
56    0            last test

What I need to do is to check how deep down a page is. I know that when it reaches parentID 0 it's finished.

I could write 3 querys and then check when is equal to 0, but it would be more nice if it's possible with only 1 query. Is it possible? How?

Here is an example:

  • ID: 56 has parent 0 and has DEPTH 0. (now 1 query)
  • ID: 55 has parent 56 then 0 and has DEPTH 1. (now 2 querys)
  • ID: 54 has parent 55 then 56 then 0 and has DEPTH 2. (now 3 querys)

I have solved it "the wrong way" (with one query each depth level) here get_depth()

The problem is that it's a recursive function and every depth requires one more query.

  • 写回答

8条回答 默认 最新

  • doupo6967 2009-10-23 08:29
    关注

    I found a very easy way to solve it, even without SQL. In my case I'm using Wordpress which have many tags and classes.

    <?php $depth = count($post->ancestors); echo $depth; ?>
    

    Simple but it works.

    Your solutions on this problem works on more than just Wordpress, if they work. Should I set my solution as the right one, what do you think?

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测