查询 MySQL 结果集是否存在特定值

Querying MySQL result set for existence of a particular value

MySQL 8.x 这里。我有下表:

[users] (users in the system)
===
user_id
user_username
user_password_enc
user_first_name
user_last_name

[events] (CRUDdable things the users can participate in)
===
event_id
event_name
event_date
event_location

[event_roles] (coarse-grained, event-specific roles that users can be assigned to)
===
event_role_id
event_role_name
event_role_label    -- ex: EVENT_ADMIN, EVENT_REP, EVENT_FOLLOWER

[event_permissions] (granular permissions that get assigned to roles)
===
event_permission_id
event_permission_name
event_permission_label    -- ex: CAN_VIEW_LOCATION, CAN_CHANGE_DATE, CAN_CHANGE_LOCATION, CAN_CANCEL_EVENT

[event_permissions_x_roles] (crosswalk table mapping permissions to roles)
===
event_permissions_x_role_id
event_permission_id
event_role_id

[event_user_roles] (which users are assigned to which event roles)
===
event_user_role_id
event_id
user_id
event_role_id

我现在想编写一个查询来询问以下问题:

Does user_id = 234 have CAN_CHANGE_LOCATION permission to event_id=123?

迄今为止我最好的尝试:

SELECT
  event_permission_label
FROM
  event_permissions_x_roles
INNER JOIN
  event_permissions
ON
  event_permissions_x_roles.event_permission_id = event_permissions.event_permission_id
WHERE
  event_role_id = (
    SELECT
      event_role_id
    FROM
      event_user_roles
    WHERE
      user_id = 234
      AND
      event_id = 123
  );

然而,这只会给我 user_id=234event_id=123event_permission_label 列表。

接近 我想要的,但我希望我的查询更进一步并检查该列表以查看 CAN_CHANGE_LOCATION 是否是该列表的一部分。我不知道是否有办法将其写成 returns 布尔值 true/false(是的,用户 确实 对给定事件具有所需的权限;或者没有用户 没有 ,等等)。谁能发现我的不足之处?

示意图:

SELECT EXISTS ( SELECT NULL
                FROM {joined tables set}
                WHERE t1.user_id = 234 
                  AND t2.event_permission_label = 'CAN_CHANGE_LOCATION'
                  AND t3.event_id = 123 )