How to represent tree structure in mySQL table

The tree has the following characteristics:

  • Each node can have several parents and several children.
  • Parent nodes can have different depths.

Example

I am trying to present a category structure such as:

Desktop and mobile applications

Desktop and Mobile Applications-> Android Applications

Desktop and Mobile Applications-> Android Applications-> Games

Desktop and Mobile Applications-> Android Applications-> Games-> Action

Desktop and mobile applications-> Games

Desktop and mobile applications-> Games-> Action

Desktop and mobile applications-> Games-> Adventure

Desktop applications

Desktop Applications-> Games

Desktop Applications-> Games-> Action

Desktop applications-> Games-> Adventure

IPhone Applications

Desktop Applications-> Games

Desktop Applications-> Games-> Action

Desktop applications-> Games-> Adventure

I tried using the nested sets algorithm, and I end up with several categories of "Games" with different category identifiers and at different depths.

Any help with this would be greatly appreciated.

+4
source share
4 answers

A simple way is to structure the table, for example:

Categories
CategoryID
Parentid
Name

Your data will look like this:

1, 0, "Desktop and Mobile Applications"
2, 1, "Android Application"
3, 2, "Games"
4, 3, "Action"
5, 1, "Games"
6, 5, "Action"
7, 5, "Adventure"

8, 0, "Desktop Applications"
9, 8, "Games"

You would request it as: select * from Categories where ParentId = 1 , which will return Android applications and games. To get subcategories of games, you would do select * from Categories where ParentId = 5 , which will return the action and adventure.


Update To associate one item with several categories, you will need another table:

xref_CategoriesItems
CategoryId
ItemId

This will allow you to associate any single item with multiple categories. Let's say you have a desktop application that should appear in desktop applications> Games and desktop and mobile applications> Games.

Your table will have the following data for item 1:
3, 1
9, 1

When you see which items were in a particular category, you should do the following:

 select I.* from items I inner join xref_CategoriesItems XCI on (XCI.ItemId = I.ItemID) WHERE (XCI.Category = @CategoryId) 

To find out which categories a particular item belongs to:

 select C.* from categories C inner join xref_CategoriesItems XCI on (XCI.CategoryId = C.CategoryId) where (XCI.ItemId = @ItemId) 

Querying for all items in a particular category is a little more complicated if you need all the child records. Basically you need to do a recursive join to xref_categories with categories in order to get children. I do not remember how to express this in the MySQL version of sql; however, it may be useful to know the following: Using a MySQL query to move rows to create a recursive tree

+1
source

What you are asking for is not really a tree, but a graph. Specifically, this is a Directional Acyclic Graph (DAG). Here's a link talking about storing DAGs in a relational database.

It may be easier to just go with the traditional tree structure for categories and allow items to be in multiple categories using the category / category link table.

0
source

An alternative approach would be to use tags for any items that you store instead of categories. In the example you are giving, this might be even more appropriate - instead of “Desktop and Mobile Applications”, “Desktop Applications” and “Mobile Applications”, you would use the tags “Desktop” and “Mobile”. Then the subject with both naturally falls into the first category.

Ref: How to store tags in MySQL tags, just one field or one for each tag?

0
source

Source: https://habr.com/ru/post/1482213/


All Articles