gpt4 book ai didi

php - 如何选择更多条件的用户

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:31 24 4
gpt4 key购买 nike

我有一个数据库模型,它按组存储用户,每个组都有其他权限。结构如图所示。

How tables looks

我想做一个 SELECT 查询,返回拥有一个或多个权限的用户。

我正在使用 medoo

public function selUserByRight($rightID)
{

return $this->db->select(
"Users",
[
"[>]GroupRight" => ["FK_Group" => "FK_Group"],
"[>]Rights" => ["GroupRight.FK_Right" => "ID"]
],
[
"Users.ID","Users.Name","Rights.ID(RightID)","Rights.Right"
],
[
"AND" =>
[
"Rights.ID" => $rightID
]
]
);
}

用法:

$result = $db->selUserByRight(1,2,3);

原始查询:

SELECT "Users"."ID","Users"."Name","Rights"."ID" 
AS "RightID","Rights"."Right"
FROM "Users"
LEFT JOIN "GroupRight" ON "Users"."FK_Group" = "GroupRight"."FK_Group"
LEFT JOIN "Right" ON "GroupRight"."FK_Right" = "Rights"."ID"
WHERE "Rights"."ID = 1

问题是,该查询返回所有用户,但我只想返回 user1(因为只有此用户拥有所有想要的权利)。

有什么解决办法吗?

最佳答案

我认为您可以尝试以下方法:

SELECT dd.ID, dd.Name, dd.FK_Group, _aa.total_rights
FROM (
SELECT aa.ID, COUNT(*) AS total_rights
FROM Users AS aa
INNER JOIN Groups AS bb
ON aa.FK_Group = bb.ID
INNER JOIN GroupRight AS cc
ON bb.ID = cc.FK_Group
GROUP BY aa.ID
HAVING COUNT(*) > 0
) AS _aa
INNER JOIN Users AS dd
ON dd.ID = _aa.ID
ORDER BY _aa.total_rights;

关于php - 如何选择更多条件的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com