MySQL recursive-query replacement in PHP

时间秒杀一切 提交于 2020-01-17 01:36:08

问题


Since it looks like recursive queries aren't possible in MySQL, I am wondering if there is a solution to get the same information that also limits the number of queries I make to the database. In my case I have what amounts to a tree and given a node, I make a path back to the root and save the name of the nodes as I go. Given a table like this:

 id | parent
-------------
 1  |   
 2  |   1
 3  |   1
 4  |   2
 5  |   2
 6  |   5

I want to select all ids on the path from 6 back to 1 (6,5,2,1). Since the total length of the path is unknown I would assume that the only way to do this is taking the results from one query and build a new query until I am back at the root. Then again it has been a couple years since I last used MySQL so it wouldn't surprise me if I am a little out of touch. Any help would be appreciated.


回答1:


Since it looks like recursive queries aren't possible in mySQL

mySQL doesn't support the 'CONNECT BY' operator, true - but you can implement recursive procedures/functions using mysql and return result sets from them.



来源:https://stackoverflow.com/questions/5399288/mysql-recursive-query-replacement-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!