hierarchical-data

MySQL Count products from all subcategories

北城以北 提交于 2020-08-11 08:59:20
问题 I have two tables; categories and products. For each category i would like to count how many products there are in all of its subcategories. I already have counted how many are in each category. Example tables are: Categories: ID ParentID ProductCount SubCategoryProducts 1 NULL 0 2 1 2 3 2 1 Products: ProductID CategoryID 123 2 124 2 125 3 So i would like my function to make: ID ParentID ProductCount SubCategoryProducts 1 NULL 0 3 2 1 2 1 3 2 1 0 It simply needs to be as a select query, no

Creating a list tree with SQLite

十年热恋 提交于 2020-08-01 06:40:10
问题 I'm trying to make a hierarchical list with PHP and an SQLite table setup like this: | itemid | parentid | name | ----------------------------------------- | 1 | null | Item1 | | 2 | null | Item2 | | 3 | 1 | Item3 | | 4 | 1 | Item4 | | 5 | 2 | Item5 | | 6 | 5 | Item6 | The lists would be built with unordered lists and allow for this type of tree structure: Item1 |_Item3 |_Item4 Item2 |_Item5 |_Item6 I've seen this done with directories and flat arrays, but I can't seem to make it work right

SQL Query to get records of parent table that have a list of child records

萝らか妹 提交于 2020-05-15 02:32:07
问题 I have two tables in an MS SQL Server 2005 database, parent and child, where the parent may be related to many child records. [Child.parent_id] is related to [parent.id]. The child table also has column [foo] I need to bring back all records in the parent table where [child.foo] matches on each of one to many parameters. For example I would like all parent records that have a [child.foo] value of 'fizz' and a [child.foo] value of 'buzz.' I have tried the below query, but it is returning

Oracle SQL nested relationship into one level

本小妞迷上赌 提交于 2020-04-17 23:06:18
问题 ColumnA ColumnB ColumnC ColumnD A B C E D C F E C H I E C W S E1 The logic should be when columnA/columnB has a record in columnC which is columnA/columnB in another record with the same columnD, it will be output as the following ColumnV ColumnW A C B C D F C F C I H I C S W S A F B F A I B I How can I write a sql to get the following: 回答1: I'm not sure if I fully understand the logic you are trying to implement, but here is SQL that creates your table and duplicates your example output. It

Oracle SQL nested relationship into one level

拈花ヽ惹草 提交于 2020-04-17 23:00:51
问题 ColumnA ColumnB ColumnC ColumnD A B C E D C F E C H I E C W S E1 The logic should be when columnA/columnB has a record in columnC which is columnA/columnB in another record with the same columnD, it will be output as the following ColumnV ColumnW A C B C D F C F C I H I C S W S A F B F A I B I How can I write a sql to get the following: 回答1: I'm not sure if I fully understand the logic you are trying to implement, but here is SQL that creates your table and duplicates your example output. It

Node.js - Convert flat json to hierarchical json without 'parent','child' attributes

前提是你 提交于 2020-04-14 18:11:05
问题 I want to build a hierarchical data structure from my sql query. After following this post for build hierarchical JSON object from flat objects, I try to create 4 level hierarchy with more attributes in object levels, but don't need children property. How can i do this? Here is my JavaScript (NodeJS) code: var levels = ["counties_id","district_id", "municipalities_id", "institutes_id"]; data.forEach(function(d){ var depthCursor = newData.counties; levels.forEach(function( property, depth ){

Generate breadcrumbs of categories stored in MySQL

半腔热情 提交于 2020-04-08 05:52:36
问题 In MySQL, I store categories this way: categories: - category_id - category_name - parent_category_id What would be the most efficient way to generate the trail / breadcrumb for a given category_id? For example breadcrumbs(category_id): General > Sub 1 > Sub 2 There could be in theories unlimited levels. I'm using php. UPDATE: I saw this article (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) about the Nested Set Model. It looks interesting, but how would you ago about

Generate breadcrumbs of categories stored in MySQL

戏子无情 提交于 2020-04-08 05:50:42
问题 In MySQL, I store categories this way: categories: - category_id - category_name - parent_category_id What would be the most efficient way to generate the trail / breadcrumb for a given category_id? For example breadcrumbs(category_id): General > Sub 1 > Sub 2 There could be in theories unlimited levels. I'm using php. UPDATE: I saw this article (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) about the Nested Set Model. It looks interesting, but how would you ago about

Generate breadcrumbs of categories stored in MySQL

此生再无相见时 提交于 2020-04-08 05:50:10
问题 In MySQL, I store categories this way: categories: - category_id - category_name - parent_category_id What would be the most efficient way to generate the trail / breadcrumb for a given category_id? For example breadcrumbs(category_id): General > Sub 1 > Sub 2 There could be in theories unlimited levels. I'm using php. UPDATE: I saw this article (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) about the Nested Set Model. It looks interesting, but how would you ago about