1

我的 SQL 数据库中有下表

用户_

userId | userName

组织_

org_id | parent_org_id_ | name

角色_

roleId | roleName

用户角色

userId | roleId

users_organization

userId |  organizationId

...现在我想要的是来自 org_Id= ?和角色名称=?我想从与具有特定角色名称的组织相关的 user_ 表中检索用户的名称。所以谁能告诉我.. 怎么做?

4

3 回答 3

2

尝试 ::

Select *
from
user_ u
INNER JOIN user_roles ur on (u.userId = ur.userId)
INNER JOIN role_ r on (ur.roleId = r.roleId)

INNER JOIN users_organization uo (u.userId = uo.userId)
INNER JOIN organization_ o on (uo.organizationId= o.orgId)
where uo.org_id =? and  r.role_name=?
于 2013-01-04T09:10:15.807 回答
1

如果我理解正确(您的问题缺少表格关系):

SELECT U.userId
    , U.userName
    FROM user_ U
    INNER JOIN users_organization UO ON UO.userId = U.userId
    INNER JOIN user_roles UR ON UR.userId = U.userId
    WHERE UO.organizationId = organizationId
        AND UR.roleName = roleName;

参数为organizationIdroleName

于 2013-01-04T09:14:42.247 回答
1

试试下面的建议。

SELECT u.userName
FROM user_ u
   , role_ r
   , users_organization uo
   , user_roles ur
WHERE r.roleName = ?
  AND uo.organizationId = ?
  AND ur.userId = u.userId 
  AND ur.roleId = r.roleId 
  AND u.userId = uo.userId 
于 2013-01-04T09:16:22.457 回答