Many-to-Many Hierarchy with Multiple Parents - PHP, MySQL

你。 提交于 2019-12-05 16:57:57

Assuming your categories cannot form cycles, like a->b->c->a, your structure is called directed acyclic graph, which is not easy to handle in SQL, but possible. Googling that should give some results, you can also start here: http://www.codeproject.com/KB/database/Modeling_DAGs_on_SQL_DBs.aspx

If your dataset is small (<10000) then you could just fetch all the data in 4 SELECT all queries and do all the category/subcategory computation in PHP.

Trees and relational databases don't go together very well :)

Do it all with php, create a function which you include into pages, that way if anything ever changes you don't have to update the table, just the file which includes the function.

I would create

books (book_id, category_id)
categories (category_id, parent_category_id, category_name, category_level)

where category.parent_category_id can be NULL. If it is NULL, then category_level will be 0 (of 1, whatever you want).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!