mysql开发分层,MySQL操作分层数据

I have MySQL table structure:

CREATE TABLE IF NOT EXISTS `categories` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`parent_id` int(10) unsigned NOT NULL DEFAULT '0',

`name` varchar(255) NOT NULL DEFAULT '',

`is_working` tinyint(1) unsigned NOT NULL DEFAULT '1',

);

which helds hierarchical data with relations of id and parent_id

I have 5 levels depth tree, like:

CATEGORY LEVEL 1

SUBCAT LEVEL 2

SUBCAT LEVEL 3

SUBCAT LEVEL 4

SUBCAT LEVEL 5

I need (the question): if I'm setting is_working = 0 for some category or subcategory, is_working is being set to 0 for all of it's subcategories.

解决方案

What I use is a different design, and though it has limitations, if you can bear them, it's very simple and very efficient.

Here is an example of taxonomic tree of birds so the hierarchy is Class/Order/Family/Genus/Species - species is the lowest level, 1 row = 1 species:

CREATE TABLE `taxons` (

`TaxonId` smallint(6) NOT NULL default '0',

`ClassId` smallint(6) default NULL,

`OrderId` smallint(6) default NULL,

`FamilyId` smallint(6) default NULL,

`GenusId` smallint(6) default NULL,

`Name` varchar(150) NOT NULL default ''

);

and the example of the data:

+---------+---------+---------+----------+---------+-------------------------------+

| TaxonId | ClassId | OrderId | FamilyId | GenusId | Name |

+---------+---------+---------+----------+---------+-------------------------------+

| 254 | 0 | 0 | 0 | 0 | Aves |

| 255 | 254 | 0 | 0 | 0 | Gaviiformes |

| 256 | 254 | 255 | 0 | 0 | Gaviidae |

| 257 | 254 | 255 | 256 | 0 | Gavia |

| 258 | 254 | 255 | 256 | 257 | Gavia stellata |

| 259 | 254 | 255 | 256 | 257 | Gavia arctica |

| 260 | 254 | 255 | 256 | 257 | Gavia immer |

| 261 | 254 | 255 | 256 | 257 | Gavia adamsii |

| 262 | 254 | 0 | 0 | 0 | Podicipediformes |

| 263 | 254 | 262 | 0 | 0 | Podicipedidae |

| 264 | 254 | 262 | 263 | 0 | Tachybaptus |

This is great because this way you accomplish all the needed operations in a very easy way, as long as the categories don't change their level in the tree.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值