这是我的表格内容和结构:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`common` /*!40100 DEFAULT CHARACTER SET utf8 */;
CREATE TABLE `apps_names` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
insert into `apps_names`(`id`,`name`) values (1,'calendar'),(2,'conference'),(3,'lunch'),(4,'devices'),(5,'confluence'),(6,'jira'),(7,'newsletter'),(8,'fotbal'),(9,'svu'),(10,'radio'),(11,'meetings'),(12,'benefits'),(13,'common');
CREATE TABLE `apps_roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_app` int(11) DEFAULT NULL,
`id_role` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
insert into `apps_roles`(`id`,`id_app`,`id_role`) values (1,11,1),(2,11,2),(3,11,3),(4,11,4),(5,11,5),(6,11,6),(7,12,7),(8,12,8),(9,12,9),(10,12,10),(11,12,11),(12,12,12),(13,12,13),(14,8,2),(15,3,11),(16,4,15),(17,4,11);
CREATE TABLE `roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
insert into `roles`(`id`,`name`) values (1,'Angajat'),(2,'Administrator'),(3,'Team-Lead'),(4,'Project-Manager'),(5,'Line-Manager'),(6,'Department-Manager'),(7,'Benefits-Admin'),(8,'Events-Admin'),(9,'Fitnes-Admin'),(10,'Pensions-Admin'),(11,'Super-Admin'),(12,'Swim-Admin'),(13,'Fitness-and-Swim-Admin'),(15,'Sim-Admin');
CREATE TABLE `users_roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert into `users_roles`(`id`,`app_id`,`user_id`,`role_id`) values (1,11,1759,2),(2,11,1767,2);
这是我的疑问:
更新查询:
SELECT r.id, r.name , role_id, id_role, ur.user_id, ur.app_id ,ap.id_app FROM roles r
INNER JOIN apps_roles ap ON
r.id = ap.id_role
LEFT JOIN users_roles ur
ON ur.role_id = ap.id_role
WHERE (user_id = '1759' OR user_id IS NULL ) AND id_app = '11'
我想提取所有角色名称,并检查哪个角色ID分配给用户。另外我想得到空值。我该怎么做?
更新这应该是想要的结果: