0

我有以下表格:

items id, principaluri, title
principals id, uri, name
members principal_id, member_id

items principaluri 是对 principals uri 字段的引用。members 表定义主体之间的关系,意味着主体的成员可以访问主体 ID 中的项目。选择如何获取主体的所有项目?

为了获得会员资格,我做

SELECT p.* 
FROM principals p
LEFT JOIN members m ON m.principal_id = p.id
WHERE p.id = m.principal_id and p.uri = 'myuri'

但现在我需要从列表中获取所有项目。

- - 更新 - -

在主体表中,我也有类似的记录

20, 'principals/admin', 'main account'
21, 'principals/admin/read', 'read access'
22, 'principals/admin/write', 'write access'
30, 'principals/test', 'main account'
31, 'principals/test/read', 'read access'
32, 'principals/test/write', 'write access'

成员表如下所示:

21,30
22,30

这意味着用户 test 对 admin 用户的项目具有读写权限。

但在项目表中,我有如下条目:

100, 'principals/admin', 'Foo'
101, 'principals/admin', 'Bar'
4

1 回答 1

1

这使用嵌套查询,但它似乎完成了这项工作:

SELECT i.*, mainp.uri mainuri, mp.memberuri, mp.linkuri access
FROM items i
LEFT OUTER JOIN principals mainp
   ON mainp.uri = i.principaluri
LEFT OUTER JOIN 
  (SELECT memberp.uri memberuri, linkp.uri linkuri
     FROM principals memberp
      INNER JOIN members m on memberp.id = m.member_id
      INNER JOIN principals linkp on m.principal_id = linkp.id) as mp
  ON mp.linkuri LIKE CONCAT(i.principaluri, '%')
WHERE mainp.uri = 'principals/test' OR mp.memberuri = 'principals/test'

小提琴:http ://www.sqlfiddle.com/#!2/7acc8/4

于 2013-01-15T16:36:06.277 回答