我有一个MySQL视图,其数据结构如下:
/==========================================\
| row_id | user_id | page_id | permissions |
|========|=========|=========|=============|
| 1 | 1 | test1 | 011 |
| 2 | 2 | test1 | 111 |
| 3 | 2 | test2 | 111 |
| 4 | 2 | test3 | 111 |
| 5 | 3 | test2 | 001 |
\==========================================/
问题是我真的只希望有人要求最多1个用户ID的数据。用户ID可能只有1行与之关联,或者可能有数百行。
我的问题:如何将视图限制为仅显示单个用户ID的数据?
实际视图SQL :(由PHPMyAdmin生成)
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `endpoint_permission_sets` AS
SELECT
`u`.`user_id` AS `user_id`,
`p`.`id` AS `permission_id`,
`p`.`endpoint` AS `endpoint`,
`p`.`individual_permissions` AS `individual_permissions`,
`p`.`collection_permissions` AS `collection_permissions`,
`p`.`global_permissions` AS `global_permissions`
FROM (
(
`other_schema`.`users` `u`
LEFT JOIN `endpoint_groups` `g`
ON (((`u`.`endpoint_group_id` = `g`.`id`) AND (`g`.`is_active` = 1)))
)
LEFT JOIN `endpoint_permissions` `p`
ON ((
((`g`.`id` = `p`.`group_id`) OR (`u`.`user_id` = `p`.`user_id`))
AND (`p`.`is_active` = 1)
))
);