MySQL - 递归树结构

MySQL - Recursing a tree structure(MySQL - 递归树结构)
本文介绍了MySQL - 递归树结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将位置链接在一起的数据库表;一个位置可以在一个位置中,也可以在另一个位置内.

I have a database table which link locations together; a location can be in a location, which can be inside another location.

location (<id>, ....)
location_parent (<location_id>, <parent_id>)

这里是 MySQL/PHP 的深度:

Here's the MySQL/PHP to go down for a depth of one:

$sql = "SELECT id FROM se_locations_services WHERE parent_locationid IN
( SELECT location_id FROM se_locations_parent WHERE parent_id = '$locationid' )";

在给定父位置的情况下,我如何仅使用 MySQL 获取其所有后代位置,无论多深?

How do I, given a parent location, gets all its descendants locations, no matter how deep, just using MySQL?

推荐答案

有一个 好看的文章 概述了管理分层数据的各种方法.我认为它为您的问题提供了完整的解决方案,并展示了各种不太简单但速度更快的方法(例如嵌套集).

There's a good-looking article over at mysql.com outlining various ways of managing hierarchical data. I think it provides a full solution to your question, and shows various less simple, but faster approaches (e.g. Nested Sets).

这篇关于MySQL - 递归树结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)