![]() |
Recursive Query with MySQL ?
Basicaly I need to fetch the root parent of any object child. (could be as low as 25 level deep into the tree structure.)
Is there a way to do it in mysql ? I've seen it in SQLServer with the "With" statement. heres my db columns: dbID idParent idChild 1 2 1 3 2 4 2 5 4 6 For example, I need to query to return 1 if I look for idChild = 6 thanks |
Think you would need a stored procedure for that, but not sure as I've never done one in MySQL.
|
Last I checked, the WITH clause was not supported in MySQL.
|
Quote:
|
|
Google Nested set model
|
Quote:
Regardless, it sounds like you real problem is that the DB design sucks and you'd just have to do multiple queries.. |
Quote:
Problem ain't speed ... it's getting root parent... |
Quote:
Are you trying to do something like: Code:
idParent* = 1 All in all, more information is needed. *I'm not including additional idParent values as of now in order to get an exact idea of what you're trying to accomplish. |
Quote:
this |
Quote:
|
Quote:
Sorry if my explanations weren't clear. (The html kind of scrw up my table) |
Quote:
@qw12er - Now let me ask.. is this in php? |
Quote:
|
Quote:
Can it be a simple matter of say... Quote:
|
Additionally, if you just want to get all idChild values and match them with idParent values, you can modify the above $query to:
$query = sprintf('SELECT idParent, idChild FROM dbtable'); Then change the while line to: while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //do stuff echo $row['idChild']; echo $row['idParent']; } |
Quote:
Yeah that's is sort of what I did but this solutions isn't clean as it could be. (makes way to much query to DB for nothing) |
Derp de derp, ta teetley tum
|
Quote:
|
All times are GMT -7. The time now is 08:05 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc