使用sql查询直接得到伪三维数组的数据,省略一些后端语言的逻辑代码
角色表
CREATE TABLE `dp_admin_role` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '角色id',
`pid` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '上级角色',
`name` varchar(32) NOT NULL DEFAULT '' COMMENT '角色名称',
`description` varchar(255) NOT NULL DEFAULT '' COMMENT '角色描述',
`menu_auth` text NOT NULL COMMENT '菜单权限',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
`status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态',
`access` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '是否可登录后台',
`default_module` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '默认访问模块',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='角色表';
用户表
CREATE TABLE `dp_admin_user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL DEFAULT '' COMMENT '用户名',
`nickname` varchar(32) NOT NULL DEFAULT '' COMMENT '昵称',
`password` varchar(96) NOT NULL DEFAULT '' COMMENT '密码',
`email` varchar(64) NOT NULL DEFAULT '' COMMENT '邮箱地址',
`email_bind` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否绑定邮箱地址',
`mobile` varchar(11) NOT NULL DEFAULT '' COMMENT '手机号码',
`mobile_bind` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否绑定手机号码',
`avatar` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '头像',
`money` decimal(11,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '余额',
`score` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '积分',
`role` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '角色ID',
`group` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '部门id',
`signup_ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '注册ip',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
`last_login_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '最后一次登录时间',
`last_login_ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '登录ip',
`sort` int(11) NOT NULL DEFAULT '100' COMMENT '排序',
`status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '状态:0禁用,1启用',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COMMENT='用户表';
SQL语句
SELECT
dp_admin_role.id as role_id,
CONCAT('[',GROUP_CONCAT(CONCAT('{user_id:',dp_admin_user.id,'}')),']') AS user_ids
FROM
dp_admin_role
left JOIN dp_admin_user ON dp_admin_role.id = dp_admin_user.role
GROUP BY
role_id
效果