1

这是我的场景

我有一个包含以下字段的权限表。

id | module | permission

1  | client | add
2  | client | edit
3  | client | delete
4  | someth | edit
5  | someth | delete

员工表

id | status | somestatus
 1 |    act | 1
 2 |    den | 1
 3 |    act | 0
 4 |    den | 1
 5 |    act | 0
 6 |    act | 1

现在我需要做的是选择具有 status="act" 和 somestatus=1 的员工,并在 module="client" 处为他们提供所有权限

所以表employee_permissions应该有这些行

id | empid | permid | permvalue
1  | 1     | 1      | 1
2  | 1     | 2      | 1
3  | 1     | 3      | 1
1  | 6     | 1      | 1
2  | 6     | 2      | 1
3  | 6     | 3      | 1

这是我尝试过的查询,我被困在这里

INSERT INTO at2_permission_employee (employee_id,permission_id) 
    SELECT at2_employee.employee_id as employee_id
         , (SELECT at2_permission.permission_id as permission_id 
            FROM at2_permission 
            where at2_permission.permission_module='client'
           ) 
    from  at2_employee  
    where at2_employee.employee_status='Active' 
      and at2_employee.employees_served_admin = 1;

我得到错误子查询返回多行,这对我来说很有意义。但我不确定如何修改查询以考虑迭代子查询返回的行

4

2 回答 2

1

如果我没记错的话,像这样:

INSERT INTO at2_permission_employee (employee_id, permission_id, permvalue)
SELECT 
  at2_employee.employee_id, 
  at2_permission.permission_id, 
  1  
FROM at2_permission cross join at2_employee
WHERE
  at2_employee.employee_status='Active' 
  and at2_employee.employees_served_admin = 1
  and at2_permission.permission_module='client';
于 2015-03-06T16:19:05.703 回答
0

有点不清楚 for 的值permvalue应该来自哪里,所以我对它进行了硬编码并同时使用了permission.idand idpermid但是这个查询应该让你知道如何完成你想要的:

insert employee_permissions (id, empid, permid, permvalue)
select p.id, e.id, p.id, 1
from employee e, permissions p
where p.module = 'client' and e.status = 'act' and e.somestatus = 1;
于 2015-03-06T16:20:04.373 回答