dongne1560 2013-09-06 16:03
浏览 79
已采纳

循环遍历MySQL数据库,直到field ='指定值'

I need some help please! Basically I have a system that has an unlimited amount of categories and the way in which it works is through unique IDs. So basically the system will find the root folder and match all subfolders based on its parent's UID. An endless loop...

But now I want to do the opposite of that in a single MySQL statement (if possible).

Basically I want it to do this.. (By the way this isn't my actual code, it's just how I want it to work)

SELECT UID FROM Table
WHERE UID = 'value'
--AND ALSO:
SELECT * FROM SameTable
WHERE UID = The Parent UID just fetched...

And do this until the UID = 'Specified Value'.

I seriously hope that makes sense!

Is it even possible? I could do it using multiple queries in a PHP loop I know, but that just feels like a long way around, and bad practice.

  • 写回答

1条回答 默认 最新

  • dongpei3245 2013-09-06 16:22
    关注

    What you have is called "Hierarchical data". You have to read on it on google. In short, there are three main ways to represent it in a 2-dimensional table:

    • Adjacency list (what you have). You scarcely can make it with single query
    • Materialized path (my favorite). Natural and readable. Not so efficient though.
    • Nested set (Most complicated) yet most powerful.

    You can choose any system you like ir stick to your current one. Single query is not Holy grail to pursue at any cost.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图