php mysql 结构_使用PHP MySQL的类别层次结构(按顺序)

bd96500e110b49cbb3cd949968f18be7.png

I am trying to ORDER my all categories and sub-categories in a hierarchy:

The main point is how to get them from MySQL ORDERLY (using POSITION field)

Cat A --> position 10

Sub-Cat 1 --> position 10

Sub_Sub_Cat 1 --> position 20

Sub_Sub_Cat 2 --> position 10

Sub_Cat 2 --> position 30

Cat B --> position 20

Cat C --> position 30

MySQL code:

CREATE TABLE IF NOT EXISTS `categories` (

`category_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,

`position` smallint(5) unsigned,

`parent_id` mediumint(8) unsigned NOT NULL DEFAULT '0'

PRIMARY KEY (`category_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

解决方案

You want to traverse the tree using SQL? That is not possible with the adjacency list model, you have to use the nested sets model. Then you can just ORDER BY left to get the whole tree in the correct order.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值