nested-sets

Move node in nested set

ε祈祈猫儿з 提交于 2019-11-28 03:42:55
I'd need a MySQL query that moves a node and all its children within a nested set. I found this site, but that function just seems so illogical - there's no universeid or treeid in a nested set model, and the code itself is just longer than what feels required. The only extra column I've got in the table is parent . I couldn't just remove and add the node again since it will loose its ID. Arturas Smorgun I see, that this topic is quite old, but anyway it's still unanswered. I got here from Google, and found no direct answer to this question. So, after a little research I found quite easy

How to render all records from a nested set into a real html tree

柔情痞子 提交于 2019-11-27 21:36:19
I'm using the awesome_nested_set plugin in my Rails project. I have two models that look like this (simplified): class Customer < ActiveRecord::Base has_many :categories end class Category < ActiveRecord::Base belongs_to :customer # Columns in the categories table: lft, rgt and parent_id acts_as_nested_set :scope => :customer_id validates_presence_of :name # Further validations... end The tree in the database is constructed as expected. All the values of parent_id , lft and rgt are correct. The tree has multiple root nodes (which is of course allowed in awesome_nested_set ). Now, I want to

Help with writing a SQL query for Nested Sets

烂漫一生 提交于 2019-11-27 18:44:39
问题 I'm storing a tree in a DB using nested sets. The table's fields are id, lft, rgt, and name. Given a node ID, I need to find all of its direct children(not grandchildren) that are themselves leaf nodes. 回答1: The article Managing Hierarchical Data in MySQL gives a great example of how to use Nested Sets, and gives examples of many common queries, including this one. here's how to find the immediate children of a node: SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM

MySQL Nested Sets - How to find parent of node?

非 Y 不嫁゛ 提交于 2019-11-26 20:59:54
问题 I have your run of the mill nested set hierarchy type setup with the following columns: table name: myset columns: id, name, lft, rgt Does anyone know a query to determine the parent of a node? I read a couple places that it's handy to also have a parent_id column in your table to keep track of this, but it seems redundant and it seems like it could get out of sync with the nested set if a query was incorrectly executed when adding/removing/moving anything within the set. 回答1: Look at this

Getting a modified preorder tree traversal model (nested set) into a <ul>

断了今生、忘了曾经 提交于 2019-11-26 18:22:17
I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site. Here is my code: function getCats($) { // retrieve all children of $parent $query = "SELECT max(rght) as max from t_categories"; $row = C_DB::fetchSingleRow($query); $max = $row["max"]; $result ="<ul>"; $query = "SELECT * from t_categories where lft >=0 and rght <= $max"; if($rs = C_DB::fetchRecordset($query)){ $p_right =""; $p_left =""; $p_diff=""; while($row = C_DB::fetchRow($rs)){ $diff = $row["rght"] -$row["lft"]; if($diff == $p_diff){ $result.= "<li>".$row[