我目前有以下sql(由typeorm生成)。
SELECT
"media"."permissionOwner" AS "media_permissionOwner",
"media"."permissionGroup" AS "media_permissionGroup",
"media"."permissionOther" AS "media_permissionOther",
"media"."id" AS "media_id",
"media"."filename" AS "media_filename",
"media"."mime" AS "media_mime",
"media"."description" AS "media_description",
"media"."length" AS "media_length",
(
SELECT
1
FROM
"user" "user"
WHERE
"user"."id" = $1 LIMIT 1
)
AS "user"
FROM
"media_object" "media"
LEFT JOIN
"media_object_groups_group" "media_groups"
ON "media_groups"."mediaObjectId" = "media"."id"
LEFT JOIN
"group" "groups"
ON "groups"."id" = "media_groups"."groupId"
LEFT JOIN
"media_object_owner_user" "media_owner"
ON "media_owner"."mediaObjectId" = "media"."id"
LEFT JOIN
"user" "owner"
ON "owner"."id" = "media_owner"."userId"
WHERE
(
"media"."permissionGroup" >= $2
AND
COUNT("groups") = 0 -- How can I accomplish this
)
OR
(
"media"."permissionGroup" >= $3
AND groups @ > user_groups
)
OR
(
owner @ > ARRAY[user]
AND "media"."permissionOwner" >= $4
)
OR "media"."permissionOther" >= $5 -- PARAMETERS: [23,4,4,4,4]
问题是,我不能使用HAVING,因为它似乎不像WHERE那样支持这种条件创建。
我在COUNT部分使用子查询进行了尝试,但后来我无法访问子选择计数查询中的“组”别名。
我什至不确定查询的其他部分是否有效,但是首先我想解决“计数”问题。