Sql 从邻接列表中选择,并从下到上排序
鉴于下表结构:Sql 从邻接列表中选择,并从下到上排序,sql,mysql,adjacency-list,Sql,Mysql,Adjacency List,鉴于下表结构: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, `description` varchar(255) NOT NULL, `parent` int(11) unsigned DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_nam
CREATE TABLE IF NOT EXISTS `roles` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`description` varchar(255) NOT NULL,
`parent` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
是否有可能以这样一种方式查询它:首先返回子对象(从下到上)
我们的目标是将它们加载到Zend_Acl的addRole
方法中,该方法要求在添加子级之前添加角色
我目前的方法是次优的(可能是错误的),需要2个查询。一个用于获取所有叶节点(其中父节点为NULL),另一个用于获取父节点按父节点排序的叶节点(DESC)
任何帮助都是非常感谢的。ORDER BY(parent不为空)ASC,parent DESC下面是我刚才给出的一个基于角色的安全答案,可能会有所帮助: 全文如下:
1, user, Login privileges, granted after account confirmation, 17
2, admin, Administrative user, has access to everything., NULL
15, unverified, Users who have not validated their email address, NULL
16, verified, Users who have validated their email address, NULL
17, guest, A guest role, an anonymous visitor with no account, NULL
18, moderator, Role for doing staff duties, 1