• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

几篇关于无限分类算法的文章_php技巧

php 搞代码 3年前 (2022-01-26) 19次浏览 已收录 0个评论

Introduction

Most users at one time or another have dealt with hierarchical data in a SQL database and no doubt learned that the management of hierarchical data is not what a relational database is intended for. The tables of a relational database are not hierarchical (like XML), but are simply a flat list. Hierarchical data has a parent-child relationship that is not naturally represented in a relational database table.

For our purposes, hierarchical data is a collection of data where each item has a single parent and zero or more children (with the exception of the root item, which has no parent). Hierarchical data can be found in a variety of database applications, including forum and mailing list threads, business organization charts, content management categories, and product categories. For our purposes we will use the following product category hierarchy from an fictional electronics store:

These categories form a hierarchy in much the same way as the other examples cited above. In this article we will examine two models for dealing with hierarchical data in MySQL, starting with the traditional adjacency list model.

The Adjacency List Model

Typically the example categories shown above will be stored in a table like the following (I'm including full CREATE and INSERT statements so you can follow along):

CREATE TABLE category(category_id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(20) NOT NULL,parent INT DEFAULT NULL);INSERT INTO categoryVALUES(1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),(7,'MP3 PLAYERS',6),(8,'FLASH',7),(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6);SELECT * FROM category ORDER BY category_id;+-------------+----------------------+--------+| category_id | name         | parent |+-------------+----------------------+--------+|      1 | ELECTRONICS     |  NULL ||      2 | TELEVISIONS     |   1 ||      3 | TUBE         |   2 ||      4 | LCD         |   2 ||      5 | PLASMA        |   2 ||      6 | PORTABLE ELECTRONICS |   1 ||      7 | MP3 PLAYERS     |   6 ||      8 | FLASH        |   7 ||      9 | CD PLAYERS      |   6 ||     10 | 2 WAY RADIOS     |   6 |+-------------+----------------------+--------+10 rows in set (0.00 sec)

In the adjacency list model, each item in the table contains a pointer to its parent. The topmost element, in this case electronics, has a NULL value for its parent. The adjacency list model has the advantage of being quite simple, it is easy to see that FLASH is a /本2文来源[email protected]搞@^&代*@码2网搞gaodaima代码child of mp3 players, which is a child of portable electronics, which is a child of electronics. While the adjacency list model can be dealt with fairly easily in client-side code, working with the model can be more problematic in pure SQL.

Retrieving a Full Tree

The first common task when dealing with hierarchical data is the display of the entire tree, usually with some form of indentation. The most common way of doing this is in pure SQL is through the use of a self-join:

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4FROM category AS t1LEFT JOIN category AS t2 ON t2.parent = t1.category_idLEFT JOIN category AS t3 ON t3.parent = t2.category_idLEFT JOIN category AS t4 ON t4.parent = t3.category_idWHERE t1.name = 'ELECTRONICS';+-------------+----------------------+--------------+-------+| lev1    | lev2         | lev3     | lev4 |+-------------+----------------------+--------------+-------+| ELECTRONICS | TELEVISIONS     | TUBE     | NULL || ELECTRONICS | TELEVISIONS     | LCD     | NULL || ELECTRONICS | TELEVISIONS     | PLASMA    | NULL || ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH || ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS  | NULL || ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL |+-------------+----------------------+--------------+-------+6 rows in set (0.00 sec)

Finding all the Leaf Nodes


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:几篇关于无限分类算法的文章_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址