hierarchical-data

Return all nodes in many-to-many hierarchal tree

扶醉桌前 提交于 2019-12-31 07:25:09
问题 Similar to this question: How do I query for all the nodes between two nodes in a tree? But I do not have a closure (flattened) table, a child can have many parents, and ID traversal is not necessarily in order. There is no limit to the nesting depth. Assume a circular reference is impossible... I would like to return all rows required to traverse the hierarchy. Assume the following table: ParentID ID RowNumber(Reference) 1 2 1 2 4 2 4 3 3 3 5 4 1 6 5 6 7 6 2 8 7 3 9 8 1 8 9 6 8 10 Given 1

SQL Server Tree Query

左心房为你撑大大i 提交于 2019-12-31 05:18:20
问题 I need some help is MS SQL Server Query. I’m not much of a DBA. I have an application with an Organization Table which is made up of a parent-child relationship: CREATE TABLE [dbo].[Organizations]( [OrgPK] [int] IDENTITY(1,1) NOT NULL, [OrgParentFK] [int] NULL, [OrgName] [varchar](200) NOT NULL, CONSTRAINT [PK__Organizations] PRIMARY KEY CLUSTERED Sample data looks like this: OrgPK, OrgParentFK, OrgName 1, 0, Corporate 2, 1, Department A 3, 1, Department B 4, 2, Division 1 5, 2, Division 2 6,

Recursive query for parent child hierarchy. Get descendants from a top node

依然范特西╮ 提交于 2019-12-31 04:26:04
问题 I have a table that stores hierarchy data in parent child format with one top node. Multiple levels, and each parent having multiple children. How can I write a recursive query to select only the parent child rows from a particular node down to the last child? Example table Parent|child 1 |2 1 |3 2 |4 2 |5 3 |6 3 |7 6 |8 How can I retrieve only rows from node 3 and all its descendants? 回答1: If your DBMS is SQL Server you can accomplish this through Common Table Expressions (CTE) using

Display hierarchical data

一笑奈何 提交于 2019-12-30 11:03:43
问题 I am playing around with a code example i found here about 'tree menu' and wanted to make this question. function tree($id) { $query = "SELECT `name`,`id` from `table` WHERE `id_parrent` = '$id'"; $result = mysql_query($query); if(mysql_num_rows($result) != 0) { echo "<ul>"; while($row = mysql_fetch_array($result)) { echo "<li>",$row[name],"</li>"; tree($row[id]); } echo "</ul>"; } } what if i want to display items in a way like this: <ul> <li>item 1</li> <li>item 2</li> <li style="padding

Display hierarchical data

一笑奈何 提交于 2019-12-30 11:03:29
问题 I am playing around with a code example i found here about 'tree menu' and wanted to make this question. function tree($id) { $query = "SELECT `name`,`id` from `table` WHERE `id_parrent` = '$id'"; $result = mysql_query($query); if(mysql_num_rows($result) != 0) { echo "<ul>"; while($row = mysql_fetch_array($result)) { echo "<li>",$row[name],"</li>"; tree($row[id]); } echo "</ul>"; } } what if i want to display items in a way like this: <ul> <li>item 1</li> <li>item 2</li> <li style="padding

Get all children by parent in mysql query

孤街浪徒 提交于 2019-12-30 11:01:00
问题 I have a table with userid, managerid as follows: id manager ------- ------- admin (NULL) james admin user james workad user creator workad Now I want all the children (descendants) for one user id. In other words, for the userid james i need the children user, workad, creator . Because james is the top parent (ancestor). Is there any query to fetch result like this in mysql... Thanks in advance. 回答1: For that you need to have a stored function : DELIMITER $$ DROP FUNCTION IF EXISTS `junk`.

Get hierarchical structure using SQL Server

岁酱吖の 提交于 2019-12-30 07:05:12
问题 I have a self-referencing table with a primary key, id and a foreign key parent_id . +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PK | NULL | IDENTITY | | parent_id | int(11) | YES | | NULL | | | name | varchar(255) | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ I have got a table as

Logic for displaying infinite category tree in nested <ul>s from Self Join Table

妖精的绣舞 提交于 2019-12-30 06:52:08
问题 Please help me solve my big problem. in my on-line shopping project i created a dynamic Category List (with Infinite Level Depth) Implemented in a Single Table in DB with Self join. the schema is like below: (source: aspalliance.com) Update I want to use a JQuery plugin to make a Multi Level Menu bar. this plugin uses <ul> and <li> elements so I should transform the DB table to <ul> and <li> . the result should like this: <ul> <li>Clothing 1 <ul> <li>Trousers 2 <ul> <li>Mens trousers 3</li>

Design Relational Database - Use hierarchical datamodels or avoid them?

空扰寡人 提交于 2019-12-30 02:27:42
问题 I'm designing a Database and I have some doubts on using Hierarchical datamodels in relational databases. If I want to deal with categories, subcategories and parent categories it is possible not to use a Hierarchical datamodels in a relational database? By another words, it is possible to deal with categories, subcategories and parent categories using the relational way of doing things? By the way, I'm using PostgreSQL. Sorry for my bad english. Best Regards, 回答1: You have a couple of

How to store a tree in SQL database

痞子三分冷 提交于 2019-12-29 18:45:00
问题 I have to store a tree in a database, so what is the best way to do this? Show the method you use and name its pros and cons. (I am using SQL Server 2005) 回答1: I found the discussion in the SQL Anti-patterns very helpfull, as it also focuses on the drawbacks of every implementation. Also, The slides 48-77 in this presentation reiterate that analisys. Bottom line, there is no such thing as a generic tree, and no silver bullet for SQL trees. You'll have to ask yourself about the data, how and