具有多个父级的多对多层次结构-PHP,MySQL [英] Many-to-Many Hierarchy with Multiple Parents - PHP, MySQL

查看:127
本文介绍了具有多个父级的多对多层次结构-PHP,MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按类别创建技术书籍列表,其中每本书籍可以属于多个类别,并且每个类别既可以是父类别又可以是子类别.

I'm trying to create a list of technology books by category, where each book can belong to more than one category, and each category can be both a parent category and a sub-category.

这是一个例子:

JavaScript
JavaScript模式
面向对象的JavaScript

JavaScript
JavaScript Patterns
Object-Oriented JavaScript

  • Ajax
    Ajax权威指南
    防弹Ajax

  • Ajax
    Ajax Definitive Guide
    Bulletproof Ajax

jQuery
学习jQuery 1.3
PHP jQuery食谱

jQuery
Learning jQuery 1.3
PHP jQuery Cookbook

PHP
简而言之,PHP
PHP jQuery食谱

PHP
PHP in a Nutshell
PHP jQuery Cookbook

Ajax
Ajax权威指南
防弹Ajax

Ajax
Ajax Definitive Guide
Bulletproof Ajax

  • XML
    XML黑客
    非废话XML
  • XML
    XML Hacks
    No-Nonsense XML

-

如您所见...

  • "PHP jQuery Cookbook"这本书分为两类:PHP和jQuery
  • "Ajax"类别既是JavaScript的子级,又是XML的父级(但XML并非JavaScript的子级)

我是这样设计数据库表的:

I've designed the database tables this way:

BOOK:          book_id, book_title  
CATEGORY:      category_id, category_name  
BOOK_CATEGORY: book_id, category_id
CATEGORY_TREE: parent_category_id, child_category_id

我已经阅读了许多其他有关MySQL层次结构数据的问题/答案,但是没有什么可以处理这种松散"层次结构.

I've read many other questions/answers on hierarchical data in MySQL, but nothing that can handle this type of "loose" hierarchy.

有人知道如何以这种方式建立列表吗?

Does anyone know how to set up a list in this way?

推荐答案

假设您的类别无法形成周期,例如a-> b-> c-> a,则您的结构称为有向无环图,这不易处理在SQL中,但可能.谷歌搜索应该会产生一些结果,您也可以从这里开始: http://www.codeproject.com/KB/database/Modeling_DAGs_on_SQL_DBs.aspx

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

这篇关于具有多个父级的多对多层次结构-PHP,MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆