从一个表中获取数据,同时根据另一个表中字段的存在/值设置字段

时间:2016-06-09 15:49:42

标签: mysql postgresql

我有以下表格:

Websites
--------
Resource (Auto incrementing primary key)
Name
URL
group_id (ID of group of people website belongs to)

WebsitePermissions
------------------
Resource_id (Referencing resource in Websites table)
GroupMember_ID (Reference to a user in group)
Is_allowed (true/false)

系统基本上如下工作:

有一个“群组”的人,群组管理员可以在控制面板中创建x个网站,分配明显的值

然后,该管理员可以查看该组中的其他人,并启用/禁用查看/使用该网站的功能,例如,On / Off开关。默认为OFF。

每个用户只需要能够查询“具有组ID的所有资源(我的组ID)”,然后有一个额外的字段来检查Permissions表以查看是否存在1:A行,而不是null即admin没有更改任何内容,2:如果有一行,则返回该用户的真/假权限

因此,理想的输出将是x用户:

Resource     Name         URL           Group_id    IS_ALLOWED
   1        Google    Google.co.uk         17          TRUE

1 个答案:

答案 0 :(得分:0)

我认为这符合您的标准:

 Select wb.Resource, wb.Name, wb.URL, wb.group_id, wperm.Is_allowed from 
      Websites wb inner join WebsitePermissions wperm on wb.Resource = wperm.Resource_id
      where GroupMember_ID = <insert your group member's ID here> 
      and wperm.Is_allowed = true