tbl user
+--------+--------+
| UserID | Name |
+--------+--------+
| 1 | John |
| 2 | Anny |
| 3 | Andy |
| 4 | Tom |
+--------+--------+
tbl rol
+--------+--------+
| RolID | Name |
+--------+--------+
| 1 | Manager|
| 2 | Lead |
| 3 | Tester |
| 4 | Develop|
+--------+--------+
tbl user_rol
+--------+--------+
| UserID | RolID |
+--------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 3 | 3 |
+--------+--------+
我有一个参数列表1,2,4
SELECT u.UserID,Name
FROM [user] u
WHERE u.UserID in(
SELECT ur2.UserID
FROM user_rol ur2
WHERE ur2.ROlID IN(1,2,4)
GROUP BY ur2.UserID
HAVING COUNT(DISTINCT ur2.RolID)=3
)
回复将是:
+--------+--------+
| UserID | Name |
+--------+--------+
| 1 | John |
但如果我的参数将是1,2,3,4不起作用,但我需要响应像以前一样
问题在于计数我不知道中间表user_rol中存在多少。
答案 0 :(得分:0)
很难从你这个问题中得知,但在我看来,我希望输出在user_rol表中列出的具有多个角色的任何用户。以下是您将如何做到这一点:
SELECT user.userid, user.name
FROM user
INNER JOIN user_rol
ON user.userid = user_rol.userid
GROUP BY user.userid, user.name
HAVING COUNT(*) > 1