如何针对以下条件编写选择查询

时间:2019-09-27 08:53:50

标签: sql database

我在数据库中有两个表

  • profilePermission =>列(配置文件ID,权限ID,配置文件名称)

  • Permissions =>列(permissionid,权限名称)

每个配置文件可以具有多个权限。 (例如,个人资料1可以具有权限1,权限2,权限3,而个人资料2可以具有权限3,权限2

我想编写一个查询,获取具有权限.permissionname =权限1和permissions.permissionname!= permission2的profileid的计数,反之亦然。

我尝试了类似的方法

select profileid  
from  CrmProfilePermissions 
join CrmPermissions on CrmProfilePermissions.permissionid=CrmPermissions.permissionid 
where name in ('permission1') and name not in('permission2') 
group by (profileid).

但是没有用。

3 个答案:

答案 0 :(得分:0)

您缺少聚合函数Highcharts.chart('container', { chart: { scrollablePlotArea: { minWidth: 2000 } }, series: [{ data }] });

count()

答案 1 :(得分:0)

我找到了正确的selectquery

select profileid  from  CrmProfilePermissions
join CrmPermissions on CrmProfilePermissions.permissionid=CrmPermissions.permissionid
where  name in ('permission1','permission2')
group by (profileid)
having count(distinct name)=1;

答案 2 :(得分:0)

我建议两种聚合级别:

select has_permission1, has_permission2, count(*),
       min(profileid), max(profileid)  -- I just include these as examples
from (select pp.profileid,
             max(case when p.name = 'permission1' then 1 else 0 end) as has_permission1,
             max(case when p.name = 'permission2' then 1 else 0 end) as has_permission2
      from CrmProfilePermissions pp join
           CrmPermissions p
           on pp.permissionid = p.permissionid
      group by pp.profileid
     ) p
group by has_permission1, has_permission2;