我有三个表:用户,角色,授权
用户
|username|lastName|firstName|....
|dan |foo |dan
|russ |bar |russ
角色
|role |roleLevel
|ROLE_ADMIN | 0
|ROLE_COMPANY_ADMIN | 20
|ROLE_MARKETING_ADMIN | 50
|ROLE_SERVICE_ADMIN | 80
|ROLE_USER | 100
当局
|username|authority
|dan |ROLE_SERVICE_ADMIN |
|dan |ROLE_USER |
|russ |ROLE_MARKETING_ADMIN|
|russ |ROLE_ADMIN |
|russ |ROLE_COMPANY_ADMIN |
我需要让所有DISTINCT用户的roleLevel高于我传入的INT及其最低的roleLevel
我现在拥有的是:
SELECT DISTINCT u.username, u.firstName, u.lastName, r.roleLevel FROM AUTHORITIES a, ROLES r, USERS u
WHERE r.role = a.authority
AND a.username = u.username
AND r.roleLevel >= #{roleLevel}
ORDER BY u.lastName, u.firstName
但是这为多个角色的用户提供了多行。如何让角色级别低于我传入的用户?
答案 0 :(得分:1)
假设用户可以有多行,您需要确定如何显示角色。
SELECT DISTINCT u.username, u.firstName, u.lastName,
max(r.roleLevel) largestRoleLevel,
min(r.roleLevel) minimumRoleLevel,
GROUP_CONCAT(r.roleLevel SEPARATOR ', ') allRoleLevels
FROM USERS u
INNER JOIN AUTHORITIES a ON a.username = u.username
INNER JOIN ROLES r ON r.role = a.authority
WHERE r.roleLevel >= #{roleLevel}
GROUP BY u.username, u.firstname, u.lastname
ORDER BY u.lastName, u.firstName
答案 1 :(得分:1)
它为您提供了多行,因为您没有指定您想要最低的行;加上你使用了大于而不是低于。
SELECT u.username, u.firstName, u.lastName, min(r.roleLevel) minroleLevel
FROM AUTHORITIES a, ROLES r, USERS u
WHERE r.role = a.authority
AND a.username = u.username
AND r.roleLevel <= #{roleLevel}
GROUP BY u.username, u.firstName, u.lastName
ORDER BY u.lastName, u.firstName
答案 2 :(得分:0)
SELECT
u.username, u.firstName, u.lastName,
min(r.roleLevel) minRoleLevel
FROM
authorities a
JOIN roles r ON r.role = a.authority
JOIN users u ON u.username = a.username
GROUP BY
u.username, u.firstName, u.lastName
HAVING
minRoleLevel >= #{roleLevel}
ORDER BY
u.lastName, u.firstName