给出以下表结构:
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 ;
是否可以通过首先返回子项(从下到上)查询它。
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
目标是将这些加载到Zend_Acl的addRole
方法中,该方法需要在添加子项之前添加角色。
我当前的方法是次优的(可能是错误的),需要2次查询。一个用于获取所有叶节点(其中父节点为NULL),另一个用于获取父节点(DESC)排序的父节点。
总是非常感谢任何帮助。
答案 0 :(得分:2)
ORDER BY (parent IS NOT NULL) ASC, parent DESC
答案 1 :(得分:0)
这是基于角色的安全性答案,我暂时回过头来说可能有所帮助:Writing an inheritance query written in SQL using an inner join?
这里有完整的脚本:http://pastie.org/1213230