查找包含所有链接行的行

发布于 2024-09-08 07:50:16 字数 390 浏览 4 评论 0原文

我有两个表:

User (id, name, etc)
UserRight (user_id, right_id)

我想找到拥有权限 1、2 和 3 的用户,但没有只拥有其中一两个权限的用户。此外,权限的数量也会有所不同,因此搜索 (1,2,3) 和 (1,2,3,4,5,6,7) 应该适用于大致相同的查询。

本质上:

SELECT *
FROM User
WHERE (
    SELECT right_id 
    FROM tblUserRight 
    WHERE user_id = id 
    ORDER BY user_id ASC
) = (1,2,3)

这在 MySQL 中可能吗?

I've got two tables:

User (id, name, etc)
UserRight (user_id, right_id)

I want to find the users who have rights 1, 2 and 3, but no users who only have one or two of these. Also, the number of rights will vary, so searches for (1,2,3) and (1,2,3,4,5,6,7) should work with much the same query.

Essentially:

SELECT *
FROM User
WHERE (
    SELECT right_id 
    FROM tblUserRight 
    WHERE user_id = id 
    ORDER BY user_id ASC
) = (1,2,3)

Is this possible in MySQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-09-15 07:50:16
SELECT u.id, u.name ...
FROM User u
JOIN UserRight r on u.id = r.user_id
WHERE right_id IN (1,2,3)
GROUP BY u.id, u.name ...
HAVING COUNT DISTINCT(right_id) = 3
SELECT u.id, u.name ...
FROM User u
JOIN UserRight r on u.id = r.user_id
WHERE right_id IN (1,2,3)
GROUP BY u.id, u.name ...
HAVING COUNT DISTINCT(right_id) = 3
冰葑 2024-09-15 07:50:16

您还可以使用 PIVOT 来执行此操作,特别是如果您需要视觉表示。我在 SQL Server 上做了这个 - 你也许可以翻译它。

Declare @User Table (id  Int, name Varchar (10))
Declare @UserRight Table (user_id Int, right_id Int)

Insert Into @User Values (1, 'Adam')
Insert Into @User Values (2, 'Bono')
Insert Into @User Values (3, 'Cher')

Insert Into @UserRight Values (1, 1)
Insert Into @UserRight Values (1, 2)
Insert Into @UserRight Values (1, 3)
--Insert Into @UserRight Values (2, 1)
Insert Into @UserRight Values (2, 2)
Insert Into @UserRight Values (2, 3)
Insert Into @UserRight Values (3, 1)
Insert Into @UserRight Values (3, 2)
--Insert Into @UserRight Values (3, 3)


SELECT *
FROM @User U
    INNER JOIN @UserRight UR
        ON U.id = UR.User_Id
PIVOT
(
    SUM (User_Id)
    FOR Right_Id IN ([1], [2], [3])
) as xx
WHERE 1=1


SELECT *
FROM @User U
    INNER JOIN @UserRight UR
        ON U.id = UR.User_Id
PIVOT
(
    SUM (User_Id)
    FOR Right_Id IN ([1], [2], [3])
) as xx
WHERE 1=1
AND [1] IS NOT NULL
AND [2] IS NOT NULL
AND [3] IS NOT NULL

You can also do this using PIVOT, especially if you want a visual representation. I did this on SQL Server - you may be able to translate it.

Declare @User Table (id  Int, name Varchar (10))
Declare @UserRight Table (user_id Int, right_id Int)

Insert Into @User Values (1, 'Adam')
Insert Into @User Values (2, 'Bono')
Insert Into @User Values (3, 'Cher')

Insert Into @UserRight Values (1, 1)
Insert Into @UserRight Values (1, 2)
Insert Into @UserRight Values (1, 3)
--Insert Into @UserRight Values (2, 1)
Insert Into @UserRight Values (2, 2)
Insert Into @UserRight Values (2, 3)
Insert Into @UserRight Values (3, 1)
Insert Into @UserRight Values (3, 2)
--Insert Into @UserRight Values (3, 3)


SELECT *
FROM @User U
    INNER JOIN @UserRight UR
        ON U.id = UR.User_Id
PIVOT
(
    SUM (User_Id)
    FOR Right_Id IN ([1], [2], [3])
) as xx
WHERE 1=1


SELECT *
FROM @User U
    INNER JOIN @UserRight UR
        ON U.id = UR.User_Id
PIVOT
(
    SUM (User_Id)
    FOR Right_Id IN ([1], [2], [3])
) as xx
WHERE 1=1
AND [1] IS NOT NULL
AND [2] IS NOT NULL
AND [3] IS NOT NULL
浊酒尽余欢 2024-09-15 07:50:16

与我的答案中指出的错误相对应,这里有一个带有计数和子查询的解决方案:

SELECT *
FROM User
WHERE 3 = (
    SELECT Count(user_id)
    FROM tblUserRight 
    WHERE right_id IN (1,2,3)
    AND user_id = User.id
)

优化器当然可以将其更改为马丁·史密斯的解决方案(即通过使用分组依据)。

In correspondance with the errors in my answer pointed out, here a solution with count and a subquery:

SELECT *
FROM User
WHERE 3 = (
    SELECT Count(user_id)
    FROM tblUserRight 
    WHERE right_id IN (1,2,3)
    AND user_id = User.id
)

An optimizer may of course change this to Martin Smith's solution (i.e. by using a group by).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文