1

我有如下三个表:

这张桌子:m_application_resources

 resourceid resource_name   menu_group_id   menu_name   creation_date   last_created_by updation_date   last_updated_by

第二个表:m_roles

roleid  rolename    description creation_date   last_created_by updation_date   last_updated_by

第三张表是:m_access_matrix

accessid    resourceid  roleid  creation_date   last_created_by updation_date   last_updated_by

表的关系是:resourceid 和 roleid

这是我的查询

select  am.accessid, ar.resource_name,rls.rolename
from m_application_resources ar, m_roles rls,m_access_matrix am 
where ar.resourceid=am.resourceid 

这将返回以下内容:

ccessid resource_name   rolename
1   DepartmentAction    Admin
1   DepartmentAction    Client
1   DepartmentAction    Doctors
2   PositionsAction     Admin
2   PositionsAction     Client
2   PositionsAction     Doctor

结果是错误的,我不知道该怎么做。

例如:DepartmentAction 应该只出现一次,并且角色名称应该是任何一个。

Sqlfiddle小提琴

请帮忙

4

2 回答 2

3

您没有条件role_id

select  am.accessid, ar.resource_name,rls.rolename
from m_application_resources ar, m_roles rls,m_access_matrix am 
where ar.resourceid=am.resourceid AND am.roleid = rls.roleid

为了更清楚起见,我会将查询转换为 ANSI SQL 语法以进行连接:

SELECT am.accessid, ar.resource_name,rls.rolename
FROM m_application_resources ar
JOIN m_access_matrix am ON ar.resourceid=am.resourceid
JOIN m_roles rls ON am.roleid = rls.roleid
于 2012-11-07T18:23:29.277 回答
2

最好在查询中显式连接表。我不是 100% 确定我了解您的表格,但这应该可以:

SELECT am.accessid, ar.resource_name, rls.rolename
FROM (m_application_resources ar INNER JOIN m_access_matrix am 
ON ar.resourceid = am.resourceid) INNER JOIN mroles rls ON am.roleid = rls.roleid
于 2012-11-07T18:29:49.650 回答