What is the best way to structure a Mysql tree table?

Asked

Viewed 95 times

2

What is the best way to structure a product table where there are infinite categories and children, such as a tree? So you can do a simple autocomplete search? Thank you

2 answers

1


A possible form would be the table for categories (tb_categorias) containing the following columns:

  • idCategory
  • idSubCategory
  • nomecategoria

If idSubCategoria = 0, she is category Father, if it’s >= 1 she is then category Son, according to the name of each category.

And in the product table:

  • idProduct
  • idCategory (where you already have category and subcategory set)

Then just recover data! ;)

0

Tatiana, you can use "Nested Set Model" as it provides very efficient queries where each item has a single parent and zero or more children.

Maybe this can help you.

  • Thank you so much! I found it very cool and it seems to be simple... I will study better!!!

  • @Tatianamartini I found it complicated! haha but we are there p/ this! Vote on my reply and helped her, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.